开发者

NMock - How to make a method with same argument types return different values?

开发者 https://www.devze.com 2023-01-29 06:09 出处:网络
I\'ve a method like this: public string MyMethod(string a, string b) { if(a == \"abcd\" && b == \"xyz\")

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&l开发者_如何学编程t;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!!


Got it!!

Need to use mock.Ordered.

All mock methods are called in an unordered manner. To make it ordered, got to use:

Using(mock.Ordered)
{
   MyMethod1(arg1, arg2);
   MyMethod2(arg2, arg1);
}

Thats it!! :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号