Ayende @ Wiki

Sometimes you have a method on your mocked object which you don't care how / if it was called, you may want to set a return value (or an exception to be thrown), but for this specific test, you just don't care. For example, you may have some interaction that you've already verified, or you are testing some other class and just need to get the right value from a method. The way to do it in Rhino Mocks is to use SetupResult.For(). Here is the code:
Test
public void SetupResultUsingOrdered()
{
   SetupResult.For(demo.Prop).Return("Ayende");
   using(mocks.Ordered())
   {
       demo.VoidNoArgs();
       LastCall.On(demo).Repeat.Twice();
   }
   mocks.ReplayAll();
   demo.VoidNoArgs();
   for (int i = 0; i < 30; i++)
   {
       Assert.AreEqual("Ayende",demo.Prop);      
   }
   demo.VoidNoArgs();
} 
When we use SetupResult.For() we tell Rhino Mocks: "I don't care about this method, it needs to do X, so just do it, but otherwise ignore it." Using SetupResult.For() completely bypasses the expectations model in Rhino Mocks. In the above example, we define demo.Prop to return "Ayende", and we can call it no matter what the currently expected method is.

What about methods returning void?

You would use LastCall.Repeat.Any(), which has identical semantics (ignore ordering, etc).

The reverse of setup result is to specify that this method should never be called (useful if you're using dynamic mocks), which is done like this:
Expect.Call(view.Ask(null,null)).IgnoreArguments().
   .Repeat.Never();
This has the same semantics as ExpectNoCall() in NMock and this tells the framework that any call to this method is an error. Notice that I still call IgnoreArguments(), since otherwise the expectation that this method will not be called would've been for a method call with null as both parameters. Like setup result and Repeat.Any(), using Repeat.Never() transcend ordering.

Note: If you want to have a method that can repeat any number of time, but still obey ordering, you can use: LastCall.On().Repeat.Times(0,int.MaxValue), this does obey all the normal rules.

Up: Rhino Mocks Documentation
Next: Rhino Mocks Mocking classes

ScrewTurn Wiki version 2.0 Beta. Some of the icons created by FamFamFam.