-
Notifications
You must be signed in to change notification settings - Fork 43
Home
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:
- Will instantiate any object, filling in any constructor dependencies with mock objects.
- Will allow you to access those mock objects after your object has been created, for verification purposes.
- Will allow you to access those mocks before the object is created, for setup purposes.
- Removes the need to create variables for each of your mock objects (like "var fake = new Mock();").
- 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());
}