如何使用 Moles 为 C# 中的方法的多次调用返回不同的值?
我一直在尝试为此找到解决方案,但要么我一直在寻找错误的搜索词,要么根本就没有我的问题的答案。
问题: 我有一个方法,我想为其编写单元测试。在这个方法中,有一个我无法真正解决的外部依赖项,因此我必须使用 Moles 来创建我的单元测试。
这种外部依赖关系由一个实例上的方法组成,该方法被调用多次(两次),第二次我想用 Moles 返回不同的值。
...
bool myVar = SomeInstance.SomeMethod(); // Here I'd like to return true
if( myVar )
...
...
bool myOtherVar = SomeInstance.SomeMethod(); // Here I'd like to return false
...
现在通常我将其设置为
MSomeInstance.SomeMethod.AllInstances.SomeMethod = @this => true;
但是我如何才能对两个调用有不同的行为?当我在上面一行后面写另一行并返回“false”时,这会“覆盖”第一行,因此我总是会得到 false 的结果。
有什么想法吗?
I've been trying to find a solution for this, but either I've been looking with the wrong search terms or there simply isn't an answer for my question yet.
Problem:
I've got a method that I'd like to write a unit test for. Within this method there is an external dependency that I can't really resolve, so I'll have to use Moles to create my unit test.
This external dependency consists of a method on an instance that is called multiple (two) times, and the second time I'd like to return a different value with Moles.
...
bool myVar = SomeInstance.SomeMethod(); // Here I'd like to return true
if( myVar )
...
...
bool myOtherVar = SomeInstance.SomeMethod(); // Here I'd like to return false
...
Now usually I set it up like
MSomeInstance.SomeMethod.AllInstances.SomeMethod = @this => true;
But how can I have different behaviours for both calls? When I write another line following the one above with "false" being returned, this "overwrites" the first one, so I'll always get false as a result.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这有点难看,但有可能:
第一个调用将返回 true,第二个调用将返回 false。
It's a bit ugly, but possible:
The first call will return true, the second false.