2

My goal is to be able to define only the parameters that are relevant for the specific tests, with immutable types in c# (strictly constructor injection, no set'ers), and have a customized fixture take care of valid values for parameters that is not specified in the test.

Scratching my head over this one - my "with" property-customations for strings, DateTime's and int's are disregarded. Example based on "Guest":

public class Guest
{
    public readonly string GuestId;
    public readonly string GivenName;
    public readonly string SurName;
    public readonly AgeCode AgeCategory;

    public Guest(string guestid, AgeCode ageCategory, string givenName, string surName)
    {
        AgeCategory = ageCategory;
        GivenName = givenName;
        SurName = surName;
        GuestId = guestid;
    }
}

I customize a Fixture instance like so:

        fixture.Customize<Guest>(composer =>
            composer
                .With(g => g.GivenName, "Finn")
                .With(g => g.GuestId, "1")
                .With(g => g.SurName, "Rasmussen")
            );

...Which works when using fixture.Create<Guest>(), but not when using fixture.Build<Guest>().With(g=>g.Surname, "Olsen"). enter image description here

Looking at the AutoFixture signatures, i understand this is because Fixture.Build<T>() instantiates a new composer, and that composer is not injected as the 'composer' instance of the Customize<T> method. Also - I understand that the first customization for a property "wins", so "overwrites" would have to be pushed in front of the customizations...

Any suggestions? Am I completely of track here, trying to use a screwdriver to hammer a nail? Or am I simply not seeing something clever and/or obvious here...

Julian
  • 1,050
  • 1
  • 12
  • 23

1 Answers1

0

For anyone else finding this: The default strategy for building a type can be changed with

Fixture.Register<TType>(Func<TType> creator)

Works like a charm...

Julian
  • 1,050
  • 1
  • 12
  • 23