Can I define an implementation of methods on Rhino Mocked?

With Rhino.Mocks, as soon as I cheat on an interface, I can:

  • Set "return" values ​​for non-empty methods on the mocked object
  • See how and with which values ​​certain methods were called with

However, is it possible to selectively define an implementation for methods on mocking objects?

Ideally, I would like to do this (RhinoImplement is the Rhino extension I hope!):

var messages = new List<IMessage>(); IBus bus = MockRepository.GenerateMock<IBus>(); bus.RhinoImplement(b => b.Send(Arg<IMessage>.Is.Anything), imess => messages.Add(imess)); //now run your test on the Class that uses IBus //now, I can inspect my local (List<IMessage>)messages collection 

Update with answer

Thanks to Patrick below, the correct code to achieve the above:

 var messages = new List<IMessage>(); IBus bus = MockRepository.GenerateMock<IBus>(); bus .Expect(b => b.Send(Arg<IMessage>.Is.Anything)) .WhenCalled(invocation => messages.Add((IMessage)invocation.Arguments[0])) .Repeat.Any() //the repeat part is because that method might be called multiple times //now run your test on the Class that uses IBus //now, I can inspect my local (List<IMessage>)messages collection 
+4
source share
2 answers
+4
source

The following code works using the newbie Rhino instead of Mock. To drown out the method with side effects.

  private IGuy DefaultDood() { var guyStub = MockRepository.GenerateStub<IGuy>(); guyStub.Expect(u => u.DrinkHouseholdProducts(Arg<string>.Is.Anything)).WhenCalled(invocation => { guyStub.JustDrank = ((string)invocation.Arguments.First()); guyStub.FeelingWell = false; } ); return guyStub; } 
0
source