我想使用创建自定义对象列表AutoFixture。我希望第一个N对象的属性设置为一个值,其余对象设置为另一个值(或简单地由Fixture的默认策略设置)。
我知道我可以使用Fixture.CreateMany<T>.With,但这会将函数应用于列表的所有成员。
其中NBuilder有一些名为TheFirstand的方法TheNext(以及其他方法)提供了此功能。它们的使用示例:
给定一个类Foo:
class Foo
{
public string Bar {get; set;}
public int Blub {get; set;}
}
可以像这样实例化一堆Foos:
class TestSomethingUsingFoo
{
/// ... set up etc.
[Test]
public static void TestTheFooUser()
{
var foosToSupplyToTheSUT = Builder<Foo>.CreateListOfSize(10)
.TheFirst(5)
.With(foo => foo.Bar = "Baz")
.TheNext(3)
.With(foo => foo.Bar = "Qux")
.All()
.With(foo => foo.Blub = 11)
.Build();
/// ... perform the test on the SUT
}
}
这给出了Foo具有以下属性的类型对象列表:
[Object] Foo.Bar Foo.Blub
--------------------------------
0 Baz 10
1 Baz 10
2 Baz 10
3 Baz 10
4 Baz 10
5 Qux 10
6 Qux 10
7 Qux 10
8 Bar9 10
9 Bar10 10
(Bar9和Bar10值代表NBuilder的默认命名方案)
有没有一种“内置”的方式来实现这一点AutoFixture?或者一种惯用的方式来设置一个像这样表现的夹具?