Using the Testable<T> Class | The Testable Pattern and Auto Mocking Explained

AutoWrockTestable

Remove the ceremony from your test classes. Easily mock your dependencies using the testable pattern.
AutoWrockTestable is an adaptation of Richard Cirerol's AutoMocking wrapper that uses StructureMap's AutoMocking assembly along with the popular MOQ mocking toolset to make it easy to test classes and automatically mock their dependencies.

Whats better than AutoMocking? Why of course, AutoWrocking!
yo.

Visual Studio integration makes composing tests a snap!

Here is how to effortlessly add test classes to your solution with all mockable dependencies mocked:

1. Download The Visual Studio Extension Here.
The extension will also install Nuget if you do not already have it and will add Structuremap, Structuremap.Automocking and Moq to your Nuget repository.

2. Create a skeleton of your implementation class.
    public class OAuthTokenService
    {
        private readonly IWebClientWrapper webClientWrapper;
        private readonly IRegistryWrapper registry;

        public OAuthTokenService(IWebClientWrapper webClientWrapper,
            IRegistryWrapper registry)
        {
            this.webClientWrapper = webClientWrapper;
            this.registry = registry;
        }
        public string GetAccessToken(string clientId, IOauthUrls oauthUrls)
        {
            return null;
        }
    }


3. Click on the "Add Testable..." menu item in Solution Explorer's "Add" context menu.
ContextMenu.png

4. Enter the name of the class you want to test. You can enter any name but the text box will auto complete using all class files open in the editor. The first class in the active file is initially selected.
Wizard.png

5. AutoWrockTestable creates a new class file with the same name as your implementation class appending "Tests" to the name and containing this code:
using AutoWrockTestable;

namespace SkyCli.Facts
{
    class OAuthTokenServiceTests
    {
        class TestableOAuthTokenService : Testable<SkyCli.OAuth.OAuthTokenService>
        {
            public TestableOAuthTokenService()
            {

            }
        }
    }
}

Writing tests using Testable<ClassToTest>

The Testable class has its dependencies automatically mocked. Now you can start to write test methods using your Testable. I like to use nested classes (a class for every method I want to test) to organize my tests. Here is how a test might look:
class OAuthTokenServiceTests
{
    class TestableOAuthTokenService : Testable<SkyCli.OAuth.OAuthTokenService>
    {
        public TestableOAuthTokenService()
        {

        }
    }

    public class GetAccessToken
    {
        [Fact]
        public void WillReturnTokenFromRegistryIfAFreshOneIsFoundThere()
        {
            var testable = new TestableOAuthTokenService();
            var registryValues = new Dictionary<string, string>();
            registryValues.Add("access_token", "token");
            registryValues.Add("expires_in", "3600");
            registryValues.Add("grant_time", DateTime.Now.Ticks.ToString());
            testable.Mock<IRegistryWrapper>().Setup(x => x.GetValues("path"))
                .Returns(registryValues);

            var result = testable.ClassUnderTest.GetAccessToken("clientId", null);

            Assert.Equal("token", result);
        }
    }
}

See Using the Testable<T> Class for a complete explanation of the Testable<T> API.

For more informatin on the Testable pattern and Auto Mocking in general see The Testable Pattern and Auto Mocking Explained.

Last edited May 16, 2012 at 6:40 AM by mwrock, version 12