Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting property value on child instance to a fixed value with Autofixture

Tags:

Is it possible to assign a fixed value to a property on a child instance when building a parent with Autofixture? It will add default values to all the properties on the child instance like a charm, but I would like to override and assign a specific value to one of the properties on the child instance.

Given this parent/child relationship:

public class Person {     public string FirstName { get; set; }     public string LastName { get; set; }      public Address Address { get; set; } }  public class Address {     public string Street { get; set; }     public int Number { get; set; }     public string City { get; set; }     public string PostalCode { get; set; } } 

I would like to assign a specific value to the City property on the address instance. I was thinking in the lines of this test code:

var fixture = new Fixture();  var expectedCity = "foo";  var person = fixture     .Build<Person>()     .With(x => x.Address.City, expectedCity)     .Create();  Assert.AreEqual(expectedCity, person.Address.City); 

That is not possible. I guess, by the reflection exception

System.Reflection.TargetException : Object does not match target type. 

...that Autofixture tries to assign the value to a City property on the Person instance instead of an Address instance.

Any suggestions?

And yes, I know that I could just add an extra step like the following:

var fixture = new Fixture();  var expectedCity = "foo";  // extra step begin var address = fixture     .Build<Address>()     .With(x => x.City, expectedCity)     .Create(); // extra step end  var person = fixture     .Build<Person>()     .With(x => x.Address, address)     .Create();  Assert.AreEqual(expectedCity, person.Address.City); 

...but was hoping for the first version or something similar (fewer steps, more terse).

Note: I'm using Autofixture v3.22.0

like image 307
Jens Andresen Avatar asked Jan 07 '15 08:01

Jens Andresen


2 Answers

For completeness, here's another way to do it:

fixture.Customize<Address>(c =>      c.With(addr => addr.City, "foo"));  var person = fixture.Create<Person>(); 

This will customize the creation of all instances of Address

If you end up using this often enough, it may be worthwhile wrapping it inside an ICustomization:

public class AddressConventions : ICustomization {     public void Customize(IFixture fixture)     {         fixture.Customize<Address>(c =>              c.With(addr => addr.City, "foo"));     } }  fixture.Customize(new AddressConventions()); 
like image 191
dcastro Avatar answered Sep 17 '22 11:09

dcastro


Not to be dismissive of the question, but the simplest solution might actually be this:

[Fact] public void SimplestThingThatCouldPossiblyWork() {     var fixture = new Fixture();     var expectedCity = "foo";     var person = fixture.Create<Person>();     person.Address.City = expectedCity;     Assert.Equal(expectedCity, person.Address.City); } 

Assignment of explicit values to properties is something most languages already excel at (C# certainly does), so I don't think AutoFixture needs a complicated DSL to reproduce half of that functionality.

like image 42
Mark Seemann Avatar answered Sep 17 '22 11:09

Mark Seemann