NMock - 如何使具有相同参数类型的方法返回不同的值?
我有一个这样的方法:
public string MyMethod(string a, string b)
{
if(a == "abcd" && b == "xyz")
return "good";
if(a == "xyz" && b == "something")
return "even better";
return "unexpected";
}
public string MainMethod()
{
string s1, s2;
if(some condition)
{
s1= "abcd";
s2 = "xyz";
}
return service.MyMethod(s1, s2);
}
我的模拟对象是这样创建的
AppObj obj = new AppObj();
Mockery mocks = new Mockery();
mockMyService = mocks.NewMock<IMyService>();
Expect.Once.On(mockMyService ).Method("MyMethod").
With("abcd", "xyz").
Will(Return.Value("good"));
obj.MainMethod();
Expect.Once.On(mockMyService ).Method("MyMethod").
With("xyz", "something").
Will(Return.Value("even better"));
obj.MainMethod();
上面代码的问题是,它总是采用第一个模拟方法的参数并返回“good”。我需要做什么才能使 NMock 对于具有不同参数值的同一方法返回不同的值?请帮忙!!
I've a method like this:
public string MyMethod(string a, string b)
{
if(a == "abcd" && b == "xyz")
return "good";
if(a == "xyz" && b == "something")
return "even better";
return "unexpected";
}
public string MainMethod()
{
string s1, s2;
if(some condition)
{
s1= "abcd";
s2 = "xyz";
}
return service.MyMethod(s1, s2);
}
My mock object is created like this
AppObj obj = new AppObj();
Mockery mocks = new Mockery();
mockMyService = mocks.NewMock<IMyService>();
Expect.Once.On(mockMyService ).Method("MyMethod").
With("abcd", "xyz").
Will(Return.Value("good"));
obj.MainMethod();
Expect.Once.On(mockMyService ).Method("MyMethod").
With("xyz", "something").
Will(Return.Value("even better"));
obj.MainMethod();
The problem with the above code is, it always takes the first mock method's parameters and returns "good". What should I need to do to make NMock return different values for a the same method with different argument values? Please help!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
知道了!!
需要使用mock.Ordered。
所有模拟方法都以无序的方式调用。要订购它,必须使用:
就是这样! :)
Got it!!
Need to use mock.Ordered.
All mock methods are called in an unordered manner. To make it ordered, got to use:
Thats it!! :)