I want to create a list of custom objects using AutoFixture
. I want the first N
objects to have a property set to one value, and the remainder to have it set to another value (or to simply be set by the Fixture
's default strategy).
I am aware that I can use Fixture.CreateMany<T>.With
, but this applies a function to all members of the list.
In NBuilder
there are methods named TheFirst
and TheNext
(among others) which provide this functionality. An example of their use:
Given a class Foo
:
class Foo
{
public string Bar {get; set;}
public int Blub {get; set;}
}
one can instantiate a bunch of Foo
s like so:
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
}
}
This gives a list of objects of type Foo
with the following properties:
[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
(The Bar9
and Bar10
values represent NBuilder
's default naming scheme)
Is there a 'built-in' way to achieve this using AutoFixture
? Or an idiomatic way to set up a fixture to behave like this?
Foo
class look like? – EssamFoo
to make it a bit clearer. If you meant the actual class in my real-life project for whichFoo
is a proxy, it's much the same: just a POCO with gettable/settablestring
,int
,DateTime
fields. – Strahan