RhinoMocks:AssertWasCalled在Stub上不起作用 [英] RhinoMocks: AssertWasCalled doesn't work on Stub

查看:102
本文介绍了RhinoMocks:AssertWasCalled在Stub上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图与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:

"IMyInterface.set_SomeProperty(任何内容;应为#1,实际为0."

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

我不明白为什么会这样.谁能帮忙吗?

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上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆