RhinoMocks:AssertWasCalled在Stub上不起作用

时间:2012-01-02 12:18:02

标签: c# unit-testing rhino-mocks assert rhino-mocks-3.5

我试图用RhinoMocks声明某个属性设置器被调用。但它没有按预期工作。

以下简化示例说明了问题。

考虑这个界面:

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(任何东西);预期#1,实际#0。”

我无法理解为什么会这样。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:7)

GenerateStub<T>返回的对象不记录属性和方法调用。如果要断言是否已调用setter,getter或方法,请改用GenerateMock<T>

// Replace
var mock = MockRepository.GenerateStub<IMyInterface>();

// with
var mock = MockRepository.GenerateMock<IMyInterface>();

// and everything should work again.
相关问题