Rhino Mocks - setting results for non-virtual methods

I'm playing around with Rhino Mocks and trying to set some dummy results on my mocking objects, so when they are called in my factory methods I don't have to worry about the data.

But I fell into a trap, the methods I want to get for the dummy results throw exceptions because they are not virtual.

I have some code like this:

using(mock.Record()){
  SetupResult.For(service.SomeMethod()).Return("hello world");
}

      

Does the method SomeMethod

have to be virtual in order to have a mocking result?

Also, what's the difference between SetupResult.For

and Expect.Call

?

+1


source to share


1 answer


Rhino Mocks uses DynamicProxy2 to create magic, so you won't be able to set expectations / results for non-virtual methods.

As for the difference between SetupResult.For

and Expect.Call

, if you want your test to fail validation if the method is not called, use Expect.Call

. If you just want to provide the result from your mock object and don't want it to not do validation if it is not called then useSetupResult.For

Thus, the following will fail:

using(mock.Record()){
    Expect.Call(service.SomeMethod()).Return("you have to run me");
}

using(mock.Replay()){
    // Some code that never calls service.SomeMethod()
}

      



And this test will not:

using(mock.Record()){
    SetupResult.For(service.SomeMethod()).Return("you don't have to run me");
}

using(mock.Replay()) {
    // Some code that never calls service.SomeMethod()
}

      

It makes sense?

+4


source







All Articles