Skip to content
darrencauthon edited this page Aug 16, 2011 · 8 revisions

AutoMoqer is an "auto-mocking" container that automatically creates any fake objects that are necessary to instantiate the class under test. It:

  1. Will instantiate any object, filling in any constructor dependencies with mock objects.
  2. Will allow you to access those mock objects after your object has been created, for verification purposes.
  3. Will allow you to access those mocks before the object is created, for setup purposes.
  4. Removes the need to create variables for each of your mock objects (like "var fake = new Mock();").
  5. Stops your tests from breaking the build when you create a new dependency.

This tool uses Moq, my favorite .Net mocking framework, for all mocks.

Here is a sample as to how it can clean up your tests:

private AutoMoqer mocker;

[SetUp]
public void Setup()
{
	mocker = new AutoMoqer();
}

[Test]
public void TestMethod()
{
	mocker.GetMock<IDataDependency>()
                    .Setup(x => x.GetData())
		.Returns("TEST DATA");

	var classToTest = mocker.Resolve<ClassToTest>();

	classToTest.DoSomething();

	mocker.GetMock<IDependencyToCheck>()
		.Verify(x=>x.CallMe("TEST"), Times.Once());
}

versus

[Test]
public void TestMethod()
{
    var dataDependencyFake = new Mock<IDataDependency>();
        dataDependencyFake.Setup(x => x.GetData()).Returns("TEST DATA");

        var dependencyToCheck = new Mock<IDependencyToCheck>();
	
        var classToTest = new ClassToTest(dataDependencyFake.Object, dependencyToCheck.Object);

        classToTest.DoSomething();

        dependencyToCheck.Verify(x=>x.CallMe("TEST"), Times.Once());
}
Clone this wiki locally