Multiple calls to a Rhino mocked method return different results

我的未来我决定 提交于 2019-12-05 12:32:36

问题


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.


回答1:


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();

or

 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.




回答2:


I'm not familiar with the syntax you're using. I would write this as:

r.ReadLine();
LastCall.Return("1,10,20").Repeat.Once();
r.ReadLine();
LastCall.Return(null).Repeat.Once();

To ensure that you're specifying the number of times that things are to be repeated. (Don't have Visual Studio to hand, syntax may not be exact.)



来源:https://stackoverflow.com/questions/1468226/multiple-calls-to-a-rhino-mocked-method-return-different-results

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!