Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I instruct AutoFixture to not bother filling out some properties?

I have a set of Data Access classes that are nested fairly deep.

To construct a list of 5 of them takes AutoFixture more than 2 minutes. 2 minutes per Unit test is way to long.

If I was coding them by hand, I would only code up the ones I need, so it would initialize quicker. Is there a way to tell AutoFixture to only do some of the properties so it can not spend time with areas of my structure I don't need?

For example:

public class OfficeBuilding {    public List<Office> Offices {get; set;} }  public class Office {    public List<PhoneBook> YellowPages {get; set;}    public List<PhoneBook> WhitePages {get; set;} }  public class PhoneBook {     public List<Person> AllContacts {get; set;}     public List<Person> LocalContacts {get; set;} }  public class Person {    public int ID { get; set; }    public string FirstName { get; set;}    public string LastName { get; set;}    public DateTime DateOfBirth { get; set; }    public char Gender { get; set; }    public List<Address> Addresses {get; set;} }  public class Addresses {    public string Address1 { get; set; }    public string Address2 { get; set; } } 

Is there a way to tell AutoFixture to create values for OfficeBuilding.Offices.YellowPages.LocalContacts, but not to bother with OfficeBuilding.Offices.YellowPages.AllContacts?

like image 821
Vaccano Avatar asked Aug 20 '13 23:08

Vaccano


People also ask

What is AutoFixture class?

AutoFixture is an open source library for . NET designed to minimize the 'Arrange' phase of your unit tests in order to maximize maintainability.

What is fixture freeze?

Freezing values Luckily we can tell our fixture to “freeze” a particular type. This means that every time we request an instance of a frozen type, we will get the same instance. You can think of it as registering a singleton instance in an IoC container.


1 Answers

The answer provided by Nikos Baxevanis provides various convention-based ways to answer the question. For completeness sake, you can also do a more ad-hoc build:

var phoneBook = fixture.Build<PhoneBook>().Without(p => p.AllContacts).Create(); 

If you want your Fixture instance to always do this, you can Customize it:

fixture.Customize<PhoneBook>(c => c.Without(p => p.AllContacts)); 

Every time that Fixture instance creates an instance of PhoneBook, it'll skip the AllContacts property, which means that you can go:

var sut = fixture.Create<OfficeBuilding>(); 

and the AllContacts property will remain untouched.

like image 117
Mark Seemann Avatar answered Oct 10 '22 17:10

Mark Seemann