StructureMap: How can i unit test the registry class?
.net, c#, structuremap
Solution
Think of a Registry class like a config file - it doesn't really make sense to test it in isolation, but you might want to test how another class responds to it. In this case, you would test how a Container behaves when given a registry, so you were on the right track by introducing the Container to your test.
In your test, you can request an IDateTimeProvider and assert that the concrete type returned is the type you expect. You can also retrieve 2 instances from the container and assert that they are the same instance (ReferenceEquals) to verify the singleton behavior.
Problem
I have a registry class like this: ``` public class StructureMapRegistry : Registry { public StructureMapRegistry() { For<IDateTimeProvider>().Singleton().Use<DateTimeProviderReturningDateTimeNow>(); } ``` I want to test that the configuration is according to my intent, so i start writing a test: ``` public class WhenConfiguringIOCContainer : Scenario { private TfsTimeMachine.Domain.StructureMapRegistry registry; private Container container; protected override void Given() { registry = new TfsTimeMachine.Domain.StructureMapRegistry(); container = new Container(); } protected override void When() { container.Configure(i => i.AddRegistry(registry)); } [Then] public void DateTimeProviderIsRegisteredAsSingleton() { // I want to say "verify that the container contains the expected type and that the expected type // is registered as a singleton } } ``` How can verify that the registry is accoring to my expectations? Note: I introduced the container because I didn't see any sort of verification methods available on the Registry class. Idealy, I want to test on the registry class directly.