Selecting The Correct Constructor When Testing With AutoFixture

The new version of AutoFixture has attribute [Greedy] to select the constructor with that has the most arguments while creating an instance of the related type. There are also other possibilities like [Modest]. See them for XUnit from the repository.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Example
{
private readonly IFoo _foo;
private readonly IBar _bar;

public Example()
{}

public Example(IFoo foo, IBar bar)
{
_foo = foo;
_bar = bar;
}
}

public class ExampleTest
{
[Theory]
[AutoMoqData] // Using AutoFixture.AutoMoqCustomization
public void MyTest([Greedy] Example sut)
{
// sut will have IFoo and IBar auto injected at this point
}
}