Archives

  • Categories

  • Testing your StructureMap Registries.

    I find that on every project I work on which uses StructureMap I include the same three unit tests per Structuremap registry.

    My TestFixture set up normally looks something like this..

            [TestFixtureSetUp]
            public void TestFixtureSetup()
            {
                // Act
                ObjectFactory.Initialize(x => x.AddRegistry(new MyRegistry()));
            }
    

    My first tests makes use of the in built testing capabilities within StructureMap. If there are any problems with your registry they should be captured by this test.

            [Test]
            public void Assert_registry_is_valid()
            {
                // Assert
                ObjectFactory.AssertConfigurationIsValid();
            }
    

    My second test isn’t really a test but is useful to have in your solution if you need to diagnose any StructureMap problems. Calling this will write out all the dependencies that are registered with your container to the output window.

             [Test]
            public void What_do_we_have()
            {
                Debug.WriteLine(ObjectFactory.WhatDoIHave());
            }
    

    My third test checks that you have more than one dependency registered with your StructureMap container. What I have found is that it is possible to configure your registry incorrectly and StructureMap won’t load any dependencies at all. However when you call the AssertConfigurationIsValid method it returns true. When you run up your application you get the ‘No Default Instance defined for PluginFamily’ exception. This tests helps you get check for this scenario.

             [Test]
            public void Assert_container_is_not_empty()
            {
                // Assert
                Assert.IsTrue(ObjectFactory.Container.Model.AllInstances.Count() > 1);
    
            }
    

    Tags:

    Leave a Reply