AutoFixture 数据对象生成器

按照一定算法随机生成一些测试用的假数据。
AutoFixture并不是对moq的替代,它只能填充对象,而不能模拟对象,但是它可以与moq框架结合实现更强大的功能。

GuGet: AutoFixture.AutoMoq

基础类型

string

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using AutoFixture;
using AutoFixture.AutoMoq;

var fix = new Fixture();
var str = fix.Create<string>();

IEnumerable

var fix = new Fixture();
fix.RepeatCount = 10;
var list = fix.Create<IEnumerable<int>>();

自定义类型

按照一定的规则生成一个对象,并可添加约束。

生成自定义字符串

var fix = new Fixture();
fix.Customizations.Add(new StringGenerator(() => GetString(5)));
var persion = fix.Create<Person>();
/// <summary>
/// 自定义字符串生成
/// </summary>
/// <param name="count"></param>
/// <returns></returns>
string GetString(int count)
{
    List<int> ints = new List<int>();
    Random rand = new Random();
    for (int i = 0; i < count; i++)
    {
        int value = rand.Next(97, 122);
        ints.Add(value);
    }
    return string.Concat(ints.ToArray());
}

时间类

原文地址:https://www.cnblogs.com/wesson2019-blog/p/14584020.html