可以使用AutoFixture的Customize()方法,为该类型单独定制Fixture,从而解决该问题。具体代码示例如下:
public class TestClass { public string Name { get; set; } public int Age { get; set; }
public TestClass(string name, int age)
{
Name = name;
Age = age;
}
}
[TestMethod]
public void TestCustomizedAutoFixture()
{
var fixture = new Fixture().Customize(new MultipleParameterConstructorCustomization
Assert.IsNull(testClass.Name);
Assert.AreEqual(0, testClass.Age);
}
public class MultipleParameterConstructorCustomization
public class MultipleParameterConstructorSpecimenBuilder
var ctors = type.GetConstructors().Where(x => x.GetParameters().Count() > 1);
if (!ctors.Any())
{
return new NoSpecimen();
}
return Activator.CreateInstance(type, ctors.First().GetParameters().Select(x => context.Resolve(x.ParameterType)).ToArray());
}
}
其中,MultipleParameterConstructorCustomization