Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 792
sduplooy Avatar asked Jan 29 '09 13:01

sduplooy


2 Answers

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

like image 137
Anuroopa Shenoy Avatar answered Sep 24 '22 03:09

Anuroopa Shenoy


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.

like image 40
jalbert Avatar answered Sep 25 '22 03:09

jalbert