带参数的AutoFixture AutoData

时间:2016-02-15 14:59:02

标签: c# .net autofixture

如何指定AutoData的参数?

例如,我的代码中包含以下内容:

var fixture = new Fixture();
fixture.Customizations.Add(
   new TypeRelay(
      typeof (IOrder),
      typeof (Order)));

我想用不同的夹具数据进行多次测试。我该怎么设置像:

.Customizations.Add().Build() / .With / .Do以便在使用AutoData时使用此指定?

1 个答案:

答案 0 :(得分:4)

你可以package various repeated AutoFixture customizations into one or more Customizations

对于特定示例,它看起来像这样:

public class OrderCustomization : ICustomization
{
    public void Customize(IFixture fixture)
    {
        fixture.Customizations.Add(
            new TypeRelay(
                typeof(IOrder),
                typeof(Order)));
    }
}

用法:

var fixture = new Fixture().Customize(new OrderCustomization());
var order = fixture.Create<IOrder>();

您可以[AutoData] creating a derived attribute that passes a Fixture instance to the appropriate base class constructor使用它们。