I use AutoFixture with FakeItEasy when I need to test a class with many dependencies, but I ned to mock only some of them. All the rest of dependencies I prefer mocking with Strict() option of FakeItEasy. In order to make my test more clean, I would like to mock only the dependencies that I want and be able to specify that all the dependencies that do not mock are created with Strict().
In the example below, I would like to be able to remove the two lines of code that create mock of IDependency2, but keep the same behavior: if the class under test accesses any method of IDependency2, an exception would be thrown.
Any idea how to do this?
[TestFixture]
public class AutoTests
{
[Test]
public void Test()
{
// Arrange
IFixture fixture = new Fixture().Customize(new AutoFakeItEasyCustomization());
var dependency1 = fixture.Freeze<IDependency1>();
A.CallTo(() => dependency1.DoSomething()).Returns(12);
// I do not want to specify these two lines
var dependency2 = A.Fake<IDependency2>(s=>s.Strict());
fixture.Register(() => dependency2);
// Act
var classUnderTest = fixture.Create<ClassUnderTest>();
var actualResult = classUnderTest.MethodUnderTest();
// Assert
Assert.That(actualResult,Is.GreaterThan(0));
}
}
public class ClassUnderTest
{
private readonly IDependency1 _dependency1;
private readonly IDependency2 _dependency2;
public ClassUnderTest(IDependency1 dependency1, IDependency2 dependency2)
{
_dependency1 = dependency1;
_dependency2 = dependency2;
}
public int MethodUnderTest()
{
return _dependency1.DoSomething()
+ _dependency2.DoSomething();
}
}
public interface IDependency1
{
int DoSomething();
}
public interface IDependency2
{
int DoSomething();
}
As a tradeoff solution, It's quite simple to implement custom ISpecimenBuilder to have all autogenerated fakes as Strict fakes. You can take a look at standard Ploeh.AutoFixture.AutoFakeItEasy.FakeItEasyRelay fakes builder to get idea what's going on behind the curtain. The modified custom builder for Strict fakes is implemented as follows:
It can be simply register with the following statement: