Object layout with constructor - Rhino Mocks

How do I make fun of an object using a constructor using Rhino Mocks?

For example, how will this object be mocked ...

public class Foo : IFoo { private IBar bar; public Foo (IBar bar) { this.bar = bar } public DoSomeThingAwesome() { //awesomeness happens here } } 
+6
c # rhino-mocks
source share
3 answers

You are not mocking Foo - you are mocking IFoo . To test Foo , you mock IBar and pass the layout to the constructor.

You should try to avoid relying on IFoo by explicitly creating Foo instances: they need to either be given a factory if IFoo somehow, or explicitly pass the IFoo tag.

+13
source share
 var myIFoo = MockRepository.GenerateStub<IFoo>(); 

you can check the awesomeness that happened through

 myIFoo.AssertWasCalled(f => f.DoSomethingAwesome()); 
+4
source share

It has been a while since I used Rhino, but I believe that you can do:

mockRespository.StrictMock<Foo>( ibarVariable )

However, this will not work if all Foo members are not virtual.

+2
source share

All Articles