Share via


AutoFakes Is Now Live at CodePlex

After many years of just talk, I’ve finally released my first Open Source Software project at CodePlex, called AutoFakes.  It’s meant for Visual Studio 11, though I suppose it would probably work in VS 10 if you have Pex/Moles installed, but definitely haven’t tested THAT.

Basically, it’s like an auto-mocking container (minus the container – there is no dependence on any IoC container). 

Let’s say you’re doing TDD and have a class that you’re building tests for.  Of course, that class is being modified over time – maybe it receives new dependencies, it changes dependencies, or even has some dependencies refactored out.  I’ve been there and when this happens, I have to go through ALL my unit tests for the class and build new mocks for the dependencies and modify the code that instantiates your code under test.  Not fun when you have dozens of tests for a particular class (this assumes you’re not using a SetUp type method, which you can’t do anyway in the case of XUnit.net).

What would be really cool is if there was a way to automatically construct the class you’re testing with all mock dependencies – let the framework figure out what those dependencies are and just build the class itself without the manual work of figuring out dependencies and mocking each one.  Well, there actually are plenty of those out there already – that is, for actual mocking frameworks like RhinoMocks, Moq, etc.  The problem is that Visual Studio 11 has a cool, new mocking and isolation framework called Fakes, and there's no existing "auto mocking" type behavior out in the Internets for this new framework, yet.  Peter Provost talks about a lot of the new VS11 unit testing features, including Fakes, here.  There is also pretty good documentation for Fakes (at least for beta software) at MSDN.

Given that you understand what using the Fakes Framework is like, you could create a test method something like following example:

         [TestMethod]
        public void ConnectorManagerCanGetConnectors()
        {
            // Arrange
            // ConnectorManager has a dependency - IConnectorRepository
            var autofake = new AutoFake<ConnectorManager>();
            var stubConnector = new StubIConnector();
            autofake.Get<StubIConnectorRepository>().GetConnectorsString =
                filter =>
                {
                    return new List<IConnector> {stubConnector, stubConnector};
                };


            // Act
            IList<IConnector> result = autofake.Target.GetConnectors("filter");

            // Assert
            Assert.AreEqual(2, result.Count);
        }

Later, if my "ConnectorManager" gains or loses additional dependencies, I don't necessarily have to come back to this test and modify it in any way - AutoFakes will detect the new dependency (an interface or abstract class, right now) and construct the class under test correctly.

If you have suggestions for additions to the framework (AutoFakes, not the general Visual Studio Fakes Framework), go to the AutoFakes Discussion or Issue Tracker, or just add a comment to this blog post.  I’ll make sure the request gets put into the appropriate area.