gpt4 book ai didi

c# - 用 rhino mocks 对一个属性进行 stub 两次

转载 作者:太空狗 更新时间:2023-10-29 20:42:13 24 4
gpt4 key购买 nike

对于某些对象,我想创建默认 stub ,以便公共(public)属性包含值。但在某些情况下,我想覆盖我的默认行为。我的问题是,我能否以某种方式覆盖已经 stub 的值?

//First I create the default stub with a default value
var foo = MockRepository.GenerateStub<IFoo>();
foo.Stub(x => x.TheValue).Return(1);

//Somewhere else in the code I override the stubbed value
foo.Stub(x => x.TheValue).Return(2);

Assert.AreEqual(2, foo.TheValue); //Fails, since TheValue is 1

最佳答案

使用 Expect 而不是 StubGenerateMock 而不是 GenerateStub 将解决这个问题:

//First I create the default stub with a default value
var foo = MockRepository.GenerateMock<IFoo>();
foo.Expect(x => x.TheValue).Return(1);

//Somewhere else in the code I override the stubbed value
foo.Expect(x => x.TheValue).Return(2);

Assert.AreEqual(1, foo.TheValue);
Assert.AreEqual(2, foo.TheValue);

关于c# - 用 rhino mocks 对一个属性进行 stub 两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7171500/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com