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 popularMOQ mocking toolset to make it easy to test classes and automatically mock their dependencies.
Whats better than AutoMocking? Why of course, AutoWrocking!
yo.
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.
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. Create a skeleton of your implementation class.
C#
Edit|Remove
csharppublic 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; }}
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; } }
2. Click on the "Add Testable..." menu item in Solution Explorer's "Add" context menu.
3. 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.
4. AutoWrockTestable creates a new class file with the same name as your implementation class appending "Tests" to the name and containing this code:
C#
Edit|Remove
csharpusing AutoWrockTestable;namespace SkyCli.Facts{ class OAuthTokenServiceTests { class TestableOAuthTokenService : Testable<SkyCli.OAuth.OAuthTokenService> { public TestableOAuthTokenService() { } } }}
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:
C#
Edit|Remove
csharpclass 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); } }}
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.