我正在尝试使用 RhinoMocks 断言调用了某个属性 setter 。但它没有按预期工作。
下面的简化示例说明了这个问题。
考虑这个接口(interface):
public interface IMyInterface
{
string SomeProperty { get; set; }
}
现在考虑以下代码:
var mock = MockRepository.GenerateStub<IMyInterface>();
mock.SomeProperty = "abc";
mock.AssertWasCalled(x => x.SomeProperty = Arg<string>.Is.Anything);
我原以为最后一行的断言会毫无问题地通过。但是,它会抛出一个 ExpectationViolationException
消息:
"IMyInterface.set_SomeProperty(anything); Expected #1, Actual #0."
我不明白为什么会这样。有人可以帮忙吗?
最佳答案
GenerateStub<T>
返回的对象不记录属性和方法调用。如果您想断言 setter、getter 或方法是否已被调用,请使用 GenerateMock<T>
相反。
// Replace
var mock = MockRepository.GenerateStub<IMyInterface>();
// with
var mock = MockRepository.GenerateMock<IMyInterface>();
// and everything should work again.
关于c# - 犀牛模拟 : AssertWasCalled doesn't work on Stub,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8700711/