I have a class Foo that uses another class Bar to do some stuff.
I'm trying to do test driven development, and therefore am writing unit tests for Foo to make sure it calls the appropriate methods on Bar, and for this purpose I'm using dependency injection and mocking Bar (using Rhino Mocks).
E.g. (in C#):
class Foo
{
private IBar bar= null;
public Foo(IBar bar)
{
this.bar= bar;
}
public void DoSomething()
{
bar.DoIt();
}
}
class FooTests
{
...
void DoSomethingCallsBarsDoItMethod()
{
IBar mockBar= MockRepository.GenerateMock<IBar>();
mockBar.Expect(b=>b.DoIt());
Foo foo= new Foo(mockBar);
foo.DoSomething();
mockBar.VerifyAllExpectations();
}
}
This all seems to be fine, but I actually want Bar to be configured with a particular parameter, and was going to have this parameter passed in via Foo's constructor. E.g. (in C#):
public Foo(int x)
{
this.bar = new Bar(x);
}
I'm not sure which is the best way to change this to be more easily testable. One options I can think of involves moving the initialization of Bar out of its constructor, e.g.
public Foo (IBar bar, int x)
{
this.bar= bar;
this.bar.Initialize(x);
}
I feel that this is making Bar harder to use though.
I suspect there may be some kind of solution that would involve using an IoC container configured to inject Foo with a mock IBar and also provide access to the created mock for expectation validation, but I feel this would be making Foo unnecessarily complex. I'm only using dependency injection to allow me to mock the dependencies for testing, and so am not using IoC containers at present, just constructing dependencies in a chained constructor call from the default constructors (although I realize this is creating more untested code) e.g.
public Foo() :
this(new Bar())
{
}
Can anyone recommend the best way to test dependent object construction/initialization?