看起来不错!但是,您也可以通过 xUnit.net扩展名以声明方式使用它。
假设测试中使用的类型定义为:
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();
}
}
}
原始测试可以写成:
[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);
}
DomainTestConventionsAttribute
定义为:
internal class DomainTestConventionsAttribute : AutoDataAttribute
{
internal DomainTestConventionsAttribute()
:base(new Fixture().Customize(new DomainTestConventions()))
{
}
}
DomainTestConventions
定义为:
internal class DomainTestConventions : CompositeCustomization
{
internal DomainTestConventions()
:base(new AutoMoqCustomization())
{
}
}
请注意,DomainTestConventions
派生自CompositeCustomization
which 基本上意味着您可以创建更多自定义项并将它们作为参数添加到基本构造函数。
您还可以阅读:
希望有帮助。