First of all, I'm going to answer this question under the assumption that TypesWithoutPublicCtrs defined as in the OP GitHub repository :
public class TypesWithoutPublicCtrs { private readonly IMyInterface _mi; public TypesWithoutPublicCtrs(IMyInterface mi) { _mi = mi; } }
The reason I explicitly call this is because the name is a red herring: it has an open constructor; it just does not have a default constructor.
In any case, AutoFixture easily copes with the lack of default constructors. The problem here is not in the TypesWithoutPublicCtrs class, but in the IMyInterface interface. Interfaces are problematic because they cannot be initialized at all.
Thus, you need to somehow map the interface to a specific class. There are various ways to do this.
One-time solution
From time to time I use this one-time solution, although I find it ugly. However, it is easy and does not require much complicated setup.
[Theory, AutoData] public void TestSomething( [Frozen(As = typeof(IMyInterface))]FakeMyInterface dummy, TypesWithoutPublicCtrs sut) {
This is not particularly pleasant because it relies on the side effect of the [Frozen] attribute, but it works as a stand-alone, one-time solution.
Convention
Nevertheless, I really want to make an agreement out of it, so the same agreement applies to all tests in the test suite. A test using such an agreement might look like this:
[Theory, MyTestConventions] public void TestSomething(TypesWithoutPublicCtrs sut) {
The [MyTestConventions] attribute may look like this:
public class MyTestConventionsAttribute : AutoDataAttribute { public MyTestConventionsAttribute() : base(new Fixture().Customize(new MyTestConventions()) {} }
The MyTestConventions class must implement the ICustomization interface. There are several ways to map IMyInterface to FakeMyInterface ; here is one:
public class MyTestConventions : ICustomization { public void Customize(IFixture fixture) { fixture.Customizations.Add( new TypeRelay(typeof(IMyInterface), typeof(FakeMyInterface))); } }
Auto-Mocking
However, you may be tired of having to create and maintain all of these fakes, so you can also include AutoFixture in the Auto-Mocking Container . There are various options for this, using Moq , NSubstitute , FakeItEasy and Rhino Mocks .