Reputation: 14720
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
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
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
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