【发布时间】:2019-05-31 18:08:23
【问题描述】:
考虑一个类
class A
{
public class NestedA
{
public string StrWithInt { get; set; }
public string Str1 { get; set; }
public string Str2 { get; set; }
}
public List<NestedA> Items { get; set; }
}
我正在使用AutoFixture 框架来生成具有随机内容的A 类的实例。 NestedA 的类属性 StrWithInt 是 string 类型,但它的值必须是数字,int 值。所以我使用With()的方法来自定义生成。
我的代码如下所示:
Random r = new Random();
Fixture fixture = new Fixture();
fixture.Customize<A.NestedA>(ob =>
ob.With(p => p.StrWithInt, r.Next().ToString())
);
var sut = fixture.Build<A>().Create();
foreach (var it in sut.Items)
{
Console.WriteLine($"StrWithInt: {it.StrWithInt}");
}
Console.ReadLine();
我得到了这样的结果。
StrWithInt: 340189285
StrWithInt: 340189285
StrWithInt: 340189285
所有值都相同。但我预计会看到这个属性的不同值。
我怎样才能达到它?
【问题讨论】:
标签: c# autofixture