AlterEgo
AlterEgo

Reputation: 81

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

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!!

Upvotes: 2

Views: 1482

Answers (1)

AlterEgo
AlterEgo

Reputation: 81

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!! :)

Upvotes: 6

Related Questions