问题描述
我试图与RhinoMocks断言某个属性设置器已被调用.但是它没有按预期工作.
I'm trying to assert with RhinoMocks that a certain property setter was called. But it's not working as expected.
下面的简化示例说明了该问题.
The following simplified example illustrates the problem.
考虑此界面:
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
:
I was expecting the assert on the last line would pass without problem. However, it is throwing an ExpectationViolationException
with this message:
我不明白为什么会这样.谁能帮忙吗?
I can't understand why this should happen. Can anyone please help?
推荐答案
GenerateStub<T>
返回的对象不记录属性和方法调用.如果要断言是否已调用setter,getter或方法,请改用GenerateMock<T>
.
The object returned by GenerateStub<T>
doesn't record property and method calls. If you want to assert if setters, getters, or methods have been called, use GenerateMock<T>
instead.
// Replace
var mock = MockRepository.GenerateStub<IMyInterface>();
// with
var mock = MockRepository.GenerateMock<IMyInterface>();
// and everything should work again.
这篇关于RhinoMocks:AssertWasCalled在Stub上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!