多次调用 Rhino 模拟方法返回不同的结果 [英] Multiple calls to a Rhino mocked method return different results
问题描述
如果我想模拟一个返回用于确定 while 循环是否应该继续的字符串的类(想象一下 read while string != null),我该如何设置期望值.我尝试了以下方法:
If I want to mock a class that returns a string that is used to determine whether while loop should continue (imagine read while string != null), how can I set the expectation. I have tried the following:
provider.Reader.Expect(r => r.ReadLine()).Return("1,10,20");
provider.Reader.Expect(r => r.ReadLine()).Return(null);
但是当它在同一个方法中被调用两次时,它在两种情况下都返回第一个字符串,而我希望它在第二次调用时返回第二个值(空).
but when it is called twice in the same method, it returns the first string on both occasions, whereas I want it to return the second value (null) if called a second time.
推荐答案
我认为您可以将重复放在您当前使用的语法的末尾.
I think you can just stick the repeat on the end of the syntax you're currently using.
provider.Reader.Expect(r => r.ReadLine()).Return("1,10,20").Repeat.Once();
provider.Reader.Expect(r => r.ReadLine()).Return(null).Repeat.Once();
或
provider.Reader.Expect(r => r.ReadLine()).Return("1,10,20").Repeat.Once();
provider.Reader.Expect(r => r.ReadLine()).Return(null);
如果您在第二次调用之后有任何想要使用第二次期望的调用.
if you have any calls beyond 2nd call that you want to use second expectation.
这篇关于多次调用 Rhino 模拟方法返回不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!