I hereby introduce a very simple no-configuration pattern for making components with dependencies unit testable. Object Quote competes with existing solutions for testability of components with dependencies and is intended to be the simplest possible solution. This will make most sense if you already understand the problem of unit testing components and classes with dependencies.
Existing Patterns for Unit Testability
- Dependency Injection or Service Location use a DI Container or a Service Locator for runtime configurability. As a side effect, they also provides a solution, albeit rather heavyweight, for test-time mockability
- The Humble Object Pattern is an approach for codebases built without DI or service locator. A class with dependencies is refactored into two classes, one of which sets up the dependencies on the other, which contains the meat (or logic) of the class. The class containing the logic is then testable.
Neither of these are cost-free. Dependency Injection requires a framework and is best considered as an application-wide architectural pattern that should be used when runtime configuration is required. In that sense it is overkill for most bespoke code where you don't need to choose at runtime between multiple implementations for an interface, and all you want is test-time mockability.
The humble object is effectively a proposal to rewrite code, albeit minimally. So it has a significant cost to it; and the end result is slightly more complex than the original.
The Object Quote Pattern
is very simple. It requires no framework, and can be applied to existing code for virtually no startup cost.
- Merrily write (or read, if it's already written) your code with hard coded dependencies.
- Uncouple the dependencies by quoting the hard-coded references with a "Soq" - a simple object quoter.
- In your unit tests, configure the Soq as a Moq Soq.
Behold. You now have decoupled, testable code. Thus:
public class MyClassThatDependsonSomething { public SomeDependency D1 { get; set; } public MyClassThatDependsonSomething() { D1 = Soq.Quote( new SomeDependency() ); } } [TestFixture] public class TestExampleCode { [Test] public void Test_something_with_dependencies() { //Arrange SomeDependency dummy = new Mock<SomeDependency>().Object; var mockSoq = new Mock<Soq>(); mockSoq.Setup(soq => soq.QuoteImplementation<SomeDependency>(It.IsAny<SomeDependency>())) .Returns(dummy); Soq.ConfigureForTest(mockSoq.Object); // Act var myClass = new MyClassThatDependsonSomething(); var result = myClass.D1; //Assert Assert.AreSame<SomeDependency>(dummy, result); } }
How it works
By default the Soq just returns the object given it.
But when configured for test, the Soq is implemented by your MoqSoq, which returns whatever you've configured it to.
Voila. Testable code with no framework needed and no extensive refactoring of existing codebase. You get started with TDD even on legacy code with minimal overhead.
Get the code and a couple of example tests from https://github.com/xcarroll/Soq. Or copy and paste the source -- just 12 lines of actual code -- from below.
The Soq Class
On Github: https://github.com/xcarroll/Soq/blob/master/Soq.cs
using System; public class Soq { private static Soq instance = new Soq(); public static T Quote<T>(T instantiatedDependency) { return instance.QuoteImplementation<T>(instantiatedDependency); } public virtual T QuoteImplementation<T>(T dependency) { return dependency; } public static void ConfigureForTest(Soq testSoq) { if (!ConfigurationIsEnabled) { throw new InvalidOperationException( "Test configuration is not enabled."); } instance = testSoq ?? new Soq(); } public static bool ConfigurationIsEnabled { get { return true; } } }