Andrew Rea - A Developer On A Few Platforms

C#, JAVA, ASP.NET, C++, ACTION SCRIPT

A nice method in StructureMap.AutoMocking – MockObjectFactory

Posted in .NET, C#, StructureMap by Andrew Rea on 4/7/2010 12:39:33 PM - CST

Recently, while writing a test for a particular unit of code, I came across a situation where the unit in question was an object which used the StructureMap.ObjectFactory.BuildUp(this) inside the constructor to resolve a SetterProperty which it had.

The easiest way I could have solved this was to simply place the dependent property as a constructor dependency instead, but due to other circumstances it was the decision that for this particular object, the dependency stayed as a property.

It is easily possible to not use this method which I am talking about, but it would mean that inside your tests you would have to clean up the ObjectFactory to ensure you left it how you found it.  Using AutoMocking, the Container will instantiated with each test meaning you will not leak any configuration into another test.

image The way I have set up some test code to provide an example of what I mean is shown on the right.  I will be testing the ConcreteClassToTest which itself has a dependency on the interface IExampleInterface .  This example is trying to show that I want to inject a Mock implementation into a Property Dependency during test.

 

 

 


    public class ConcreteClassToTest
    {
        [StructureMap.Attributes.SetterProperty]
        public IExampleInterface ExampleInterface { get; set; }

        public ConcreteClassToTest()
        {
            ObjectFactory.BuildUp(this);
        }

        public string OutputExampleValue()
        {
            return ExampleInterface.GetStringValue();
        }
    }

 

This simple class once instantiated will call on the ObjectFactory to provide an implementation for the interface IExampleInterface and inject it.  In my test, I have created a Mock implementation of this and called it FakeExampleInterfaceImplementation, bit long I know… 


    [TestFixture]
    public class TestConcreteClassToTest : StructureMap.AutoMocking.RhinoAutoMocker<ConcreteClassToTest>
    {
        [SetUp]
        public void SetUp()
        {
            MockObjectFactory();
            Container.Configure(x => x.For<IExampleInterface>().Use<FakeExampleInterfaceImplementation>());
        }

        [Test]
        public void Test_AutoMocker_Container_Is_Same_As_ObjectFactory()
        {
            Assert.AreSame(Container, ObjectFactory.Container);
        }

        [Test]
        public void Test_Fake_Implementation_is_used()
        {
            Assert.AreEqual("TEST", ClassUnderTest.OutputExampleValue());
        }
    }

 

I am using NUnit as my testing framework, and in my test above I am inheriting from RhinoAutoMocker, which itself relies on RhinoMocks but is implemented inside the StructureMap project, inside the AutoMocking assembly.  After the first statement is executed, any call to ObjectFactory.Container will in turn be referencing the RhinoAutoMocker Container.  The method itself inside the StructureMap source code is as follows:


        /// <summary>
        /// Use this with EXTREME caution.  This will replace the active "Container" in accessed
        /// by ObjectFactory with the AutoMockedContainer from this instance
        /// </summary>
        public void MockObjectFactory()
        {
            ObjectFactory.ReplaceManager(_container);
        }

 

The actual method ReplaceManager method is internal, so you should be using the above method to achieve the same result.


 internal static void ReplaceManager(Container container)
        {
            _container = container;
        }

image

Cheers for now,

Andrew

Comments

None.

Add Comment

Login using
Google Yahoo flickr AOL
and more
Or provide your details
Please enter your name. Please enter a valid email. Please enter a valid website.
Please supply a comment.
© Copyright 2010 Powered by AtomSite 1.4.0.0