sduplooy
sduplooy

Reputation: 14720

How do I combine two interfaces when creating mocks?

We are using Rhino Mocks to perform some unit testing and need to mock two interfaces. Only one interface is implemented on the object and the other is implemented dynamically using an aspect-oriented approach. Is there an easy way to combine the two interfaces dynamically so that a mock can be created and the methods stubbed for both interfaces?

Upvotes: 18

Views: 5316

Answers (3)

Anuroopa Shenoy
Anuroopa Shenoy

Reputation: 393

Using Rhino Mocks

var mock = MockRepository.GenerateMock<IFirst, ISecond>();
mock.Stub(m => m.FirstProperty).PropertyBehavior();
((ISecond)mock).Stub(k=> k.SecondProperty).PropertyBehavior();

Found and used the information from http://www.richard-banks.org/2010/08/mocking-comparison-part-11-multiple.html

Upvotes: 18

sduplooy
sduplooy

Reputation: 14720

A mock with multiple interfaces using Rhino Mocks can be generated like so:

var mocker = new MockRepository();
var mock = mocker.CreateMultiMock<IPrimaryInterface>(typeof(IFoo), typeof(IBar));
mocker.ReplayAll();

Upvotes: 3

jalbert
jalbert

Reputation: 3097

It's not dynamic, but certainly easy: create an interface within your testing assembly which does nothing other than implementing the other two interfaces:

internal interface ICombined : IFirstInterface, ISecondInterface {}

Then mock ICombined.

Upvotes: 14

Related Questions