RhinoMocks: AssertWasCalled doesn't work on Stub

狂风中的少年 提交于 2019-12-11 02:44:52

问题


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.

Consider this interface:

public interface IMyInterface
{
    string SomeProperty { get; set; }
}

And now consider the following code:

var mock = MockRepository.GenerateStub<IMyInterface>();
mock.SomeProperty = "abc";

mock.AssertWasCalled(x => x.SomeProperty = Arg<string>.Is.Anything);

I was expecting the assert on the last line would pass without problem. However, it is throwing an ExpectationViolationException with this message:

"IMyInterface.set_SomeProperty(anything); Expected #1, Actual #0."

I can't understand why this should happen. Can anyone please help?


回答1:


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.


来源:https://stackoverflow.com/questions/8700711/rhinomocks-assertwascalled-doesnt-work-on-stub

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!