We are using StructureMap in a new software development project. One of the team members has implemented a unit test that basically tests the StructureMap container configuration. It does this by doing the following;
- Counts number of instances of assemblies that are configured for classes in our application namespace.
- Defines expected instances at the class level
- Asserts that expected instances match total found instances.
- Asserts that expected instances match those defined in the test
An example of this is;
var repositories = container.GetAllInstances<IEnvironmentRepository>();
Assert.AreEqual(1, repositories .Count());
foundInstances = foundInstances + repositories .Count();
We also have 'unit tests' for the following class;
public MyClass(IEnvironmentRepository environmentRepository)
{
}
In these tests, we mock IEnvironmentRepository, so would not be injecting it from the container as would happen in the live system.
A colleague ignored the unit test on the structuremap config with a comment along the line of "Unit test only tests it's own configuration". This was obviously the purpose of the test and in my opinion is perfectly valid. I asked the guy who ignored the test to remove the structuremap configuration for IEnvironmentRepository
(with the test still ignored) and run the full unit test suite, they all passed. We then ran the application and it fell over because the container configuration was now invalid. In my opinion, this proved the value of the test, my colleague still disagreed. He simply stated that we should not be testing configuration, but I deem this to be well within the remit of a unit test.
So a number of questions;
- Is it a valid unit test - We are testing the configuration of our container, not that structuremap works (but I can see the overlap)
- If not, how can you validate the configuration without testing it. How can you stop someone accidentally deleting a required line of code and checking it in?
- Should the
MyClass
unit test resolve the instance ofIEnvironmentRepository
from the container and pass this in?