为RangedAttribute修饰的string类型的模型属性创建fixture

时间:2017-11-08 16:07:26

标签: autofixture

是否有一种通用方法来处理此方案中 AutoFixture 的行为?我想在此测试下自定义夹具,所以每次调用fixture.Create<MyModel>();时,我都会得到一个完全填充的模型,其中包含字符串属性设置,而不是调用Build<MyModel>()并且每次处理所有开销。

当使用RangeAttribute修饰int类型的模型属性时,它被正确模拟,但是当基础类型为字符串时,会抛出AutoFixture.ObjectCreationException异常,说明:

'AutoFixture was unable to create an instance from AutoFixture.Kernel.RangedNumberRequest, most likely because it has no public constructor, is an abstract or non-public type.

例如:

public class MyModel
{
    [Required]
    [Range(1, 65535)]
    public string Port { get; set; }
}

var fixture = new Fixture();
var a = fixture.Create<MyModel>();

在3.51.0中第一次调用Create(),但后续调用引发异常。现在在4.0.0-rc1(我正在使用的版本)中,第一个调用抛出。

0 个答案:

没有答案