Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Omitting a specific field with CreateMany from AutoFixture

I want to create "many" instances of foo :

var fixture = new Fixture();
var expectedFoos = fixture.CreateMany<Foo>();

The problem is, Foo is an Entity Framework entity, with relations that I don't want to create. If I needed only one instance, I could do that :

var fixture = new Fixture();
var expectedFoo = fixture.Build<Foo>()
                         .Without(foo => foo.Relation1);
                         .Without(foo => foo.Relation2);

But how can I easily create multiple instances satisfying this condition ? I've read about specimen builders but it seems really overkill here.

I'm looking for something as simple as that (doesn't compile because BuildMany doesn't exist) :

var fixture = new Fixture();
var expectedFoos = fixture.BuildMany<Foo>()
                          .Without(foo => foo.Relation1);
                          .Without(foo => foo.Relation2);
like image 538
KeatsPeeks Avatar asked Mar 07 '14 23:03

KeatsPeeks


2 Answers

That's what Customize is for :

var fixture = new Fixture();
fixture.Customize<Foo>(o => o
       .Without(foo => foo.Relation1);
       .Without(foo => foo.Relation2));
var expectedFoos = fixture.CreateMany<Foo>();
like image 63
KeatsPeeks Avatar answered Sep 20 '22 16:09

KeatsPeeks


Using Customize is definitely the right answer. However, just for the sake of documentation, Build will work too:

var expectedFoos = fixture.Build<Foo>()
                          .Without(foo => foo.Relation1)
                          .Without(foo => foo.Relation2)
                          .CreateMany();
like image 32
Enrico Campidoglio Avatar answered Sep 20 '22 16:09

Enrico Campidoglio