AutoFixture依从于FluentValidation
创始人
2024-11-12 14:00:18
0

在使用AutoFixture生成测试数据时,可以结合FluentValidation对数据进行校验,确保生成的数据符合指定的规则。

示例代码如下:

使用FluentValidation定义数据模型的规则:

public class Order
{
    public int Id { get; set; }
    public string CustomerName { get; set; }
    public DateTime OrderDate { get; set; }
    public decimal TotalAmount { get; set; }
}

public class OrderValidator : AbstractValidator
{
    public OrderValidator()
    {
        RuleFor(order => order.Id).GreaterThan(0);
        RuleFor(order => order.CustomerName).NotEmpty();
        RuleFor(order => order.OrderDate).Must(BeAValidDate);
        RuleFor(order => order.TotalAmount).GreaterThan(0);
    }

    private bool BeAValidDate(DateTime date)
    {
        return date != default(DateTime);
    }
}

利用AutoFixture生成符合规则的测试数据:

var fixture = new Fixture().Customize(new AutoMoqCustomization());

var order = fixture.Build()
                    .With(o => o.TotalAmount, 100m)
                    .Create();
                    
var validator = new OrderValidator();
var result = validator.Validate(order);
Assert.True(result.IsValid);

以上代码保证了在生成Order实例时,符合OrderValidator的规则。如果不符合,Validator.Validate()方法会返回Invalid的ValidationResult。

相关内容

热门资讯

透视规律!wepoker透视脚... 透视规律!wepoker透视脚本下载,WePoKer辅助器(透视)总结教程(有挂助手)-哔哩哔哩一、...
有玩家发现!wepoker智能... 有玩家发现!wepoker智能辅助插件,wpk透视是真的吗(透视)必备教程(有挂工具)-哔哩哔哩1、...
更值得关注的是!哈糖大菠萝怎么... 更值得关注的是!哈糖大菠萝怎么开挂,拱趴大菠萝万能挂(透视)必备教程(果真有挂)-哔哩哔哩1)哈糖大...
此事备受玩家关注!hhpoke... 此事备受玩家关注!hhpoker辅助挂是真的吗,拱趴大菠萝辅助方法(透视)揭幕教程(竟然有挂)-哔哩...
围绕透视问题!wepoker国... 围绕透视问题!wepoker国外版透视,wepoker怎么获得好牌(透视)揭幕教程(有挂实锤)-哔哩...
透视挂!wepokerplus... 透视挂!wepokerplus辅助,we-poker辅助(透视)教你教程(果真有挂)-哔哩哔哩;1、...
透视ai代打!newpoker... 透视ai代打!newpoker脚本,wepoker私人局透视插件(透视)辅助教程(发现有挂)-哔哩哔...
据文件显示!wepoker透视... 据文件显示!wepoker透视脚本免费使用视频,德州局透视脚本(透视)详细教程(有挂总结)-哔哩哔哩...
透视规律!智星菠萝有挂吗,we... 透视规律!智星菠萝有挂吗,wepoker有辅助插件吗(透视)详情教程(有挂秘籍)-哔哩哔哩1、点击下...
目前来看!wpk私人辅助,约局... 目前来看!wpk私人辅助,约局吧透视挂下载(透视)辅助教程(有挂详情)-哔哩哔哩小薇(辅助器软件下载...