It's looking good! However, you can also use it declaratively with the xUnit.net extension.
Assuming that the types used in the test are defined as:
public class CardHolderCustomer
{
}
public interface ICustomerAdapter
{
CardHolderCustomer BuildCustomer();
}
public class CardHolderViewModel
{
private readonly ICustomerAdapter adapter;
public CardHolderViewModel(ICustomerAdapter adapter)
{
if (adapter == null)
throw new ArgumentNullException("adapter");
this.adapter = adapter;
}
public CardHolderCustomer Customer
{
get
{
return this.adapter.BuildCustomer();
}
}
}
The original test can be written as:
[Theory, DomainTestConventions]
public void CustomerPropertyIsCorrect2(
CardHolderCustomer expected,
[Frozen]Mock<ICustomerAdapter> builderStub,
CardHolderViewModel sut)
{
builderStub
.Setup(x => x.BuildCustomer())
.Returns(expected);
var actual = sut.Customer;
Assert.Equal(expected, actual);
}
The DomainTestConventionsAttribute
is defined as:
internal class DomainTestConventionsAttribute : AutoDataAttribute
{
internal DomainTestConventionsAttribute()
:base(new Fixture().Customize(new DomainTestConventions()))
{
}
}
The DomainTestConventions
is defined as:
internal class DomainTestConventions : CompositeCustomization
{
internal DomainTestConventions()
:base(new AutoMoqCustomization())
{
}
}
Note that DomainTestConventions
derives from CompositeCustomization
which basically means that you can create more Customizations and add them as parameters to the base constructor.
You may also read:
- The order of AutoFixture Customizations matter
- AutoData Theories with AutoFixture
- Keep your unit tests DRY with AutoFixture Customizations
- AutoFixture, xUnit.net, and Auto Mocking
Hope that helps.