Third Shelf

Mocking multiple interfaces using Rhino Mocks

Posted in .net, visual studio by Sydney du Plooy on June 6, 2010

A while back on StackOverflow I asked a question on how to create a mock object with Rhino Mocks that implements multiple interfaces. In other words, I want to generate a mock that implements more than one interface. This baffled me for a while and I was shown the light by one of my colleagues.

A multi-mock is created like so:

var mocker = new MockRepository();
var mock = mocker.CreateMultiMock<IPrimaryInterface>(typeof(IFoo), typeof(IBar));
mock.Expect(x => x.AnswerToUniverse()).Return(42);
mocker.ReplayAll();

Note the call to ReplayAll. Without this call the mock will not be setup with the intended values.

One Response

Subscribe to comments with RSS.

  1. Ariel said, on March 16, 2011 at 19:04

    Exactly what I was looking for. Great tip….saved my day :). regards.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 144 other followers