This project is read-only.
Home | The Testable Pattern and Auto Mocking Explained

Using the Testable<T> class API

Constructors

Testable()
This is the constructor you will most commonly use.

Testable(Action<Testable<T>> setup)
Use this constructor if you want to setup the mocks up front in the constructor.

Example:
var testable = new Testable<MyClassToTest>(
    x => {
            x.Mock<MyDependencyClass>().Setup(y => y.MyBoolProperty).Returns(true);
        }
    )

Methods

Mock<T>()
Returns a MOQ mock of T.


Inject<T>(T type)
Injects a concrete instance of T for the terstable to use instead of a Mock.

Example:
testable.Inject<MyDependencyClass>(new MyDependencyClass());

InjectArray<T>(T[] types)
Allows you to "inject" an array of known objects for an argument of type T[] in the ClassUnderTest.

Properties

T ClassUnderTest
Returns the class being tested with all dependencies mocked.

AutoMockedContainer AutoMockContainer
Returns the StructureMap DI container tracking the dependencies.

Example:
class TestableSpriteGenerator : Testable<SpriteGenerator>
{
    public TestableSpriteGenerator()
    {
        AutoMockContainer.Configure(x => { 
            x.SelectConstructor(() => new SpriteGenerator(null, null));
            x.For<Func<IImageLoader, Func<byte[], string>, ISpriteManager>>().
                Use((loader, urlGenerator) => 
                    AutoMockContainer.GetInstance<ISpriteManager>());
        });
    }
}

Last edited May 15, 2012 at 4:25 PM by mwrock, version 6

Comments

No comments yet.