Rhino Mocks:如何从模拟对象方法返回条件结果
我想做类似以下的事情,但似乎无法完全正确地获得 Do 方法的语法。
var sqr = new _mocks.CreateRenderer<ShapeRenderer>();
Expect.Call(sqr.CanRender(null)).IgnoreArguments().Do(x =>x.GetType() == typeof(Square)).Repeat.Any();
所以基本上,我想设置 sqr.CanRender() 方法,如果输入是 Square 类型,则返回 true,否则返回 false。
I would like to do something like the following but can't seem to get the syntax for the Do method quite right.
var sqr = new _mocks.CreateRenderer<ShapeRenderer>();
Expect.Call(sqr.CanRender(null)).IgnoreArguments().Do(x =>x.GetType() == typeof(Square)).Repeat.Any();
So basically, I would like to set up the sqr.CanRender() method to return true if the input is of type Square and false otherwise.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您在找这个吗?
编辑:答案在精神上是正确的,但原始语法不太有效。
Are you looking for this?
EDIT: The answer was correct in spirit bu the original syntax did not quite work.
如果您无法使用 .Net Framework 3.5(克里斯蒂安的回答),因此无权访问 System.Func 委托,那么您需要定义自己的委托。
添加到类成员:
期望变为:
If you aren't able to use .Net Framework 3.5 (required by Cristian's answer) and therefore don't have access to the System.Func delegates then you'll need to define your own delegate.
Add to the class member:
The expectation becomes:
从 Rhino Mocks 3.5 开始,您现在可以执行以下操作:
查看这篇 wiki 文章 了解更多信息。
As of Rhino Mocks 3.5 you can now do the following:
Look at this wiki article for more information.