How do I mock an object with a constructor using Rhino Mocks?
For example how would this object be mocked…
public class Foo : IFoo
{
private IBar bar;
public Foo (IBar bar)
{
this.bar = bar
}
public DoSomeThingAwesome()
{
//awesomeness happens here
}
}
Best Answer
You don't mock
Foo
- you mockIFoo
. To testFoo
itself, you mockIBar
and pass the mock into the constructor.You should try to avoid having things which rely on
IFoo
explicitly constructing instances ofFoo
: they should either be given a factory ifIFoo
somehow, or have anIFoo
explicitly passed to them.