Can AutoFixture execute a delegate at object creation time?

Go To StackoverFlow.com

6

I'm looking to customize the creation-time behavior of AutoFixture such that I can set up some dependent objects after the properties of the fixture have been generated and assigned.

For example, suppose I have a method that customizes a User because its IsDeleted property always has to be false for a certain set of tests:

public class User
{
   public int Id { get; set; }
   public string Name { get; set; }
   public bool IsDeleted { get; set; }
}

public static ObjectBuilder<User> BuildUser(this Fixture f)
{
   return f.Build<User>().With(u => u.IsDeleted, false);
}

(I hand an ObjectBuilder back to the test so it can further customize the fixture if necessary.)

What I'd like to do is automatically associate that user with an anonymous collection by its Id at creation time, but I can't do this as-is because Id has not been generated by the time I hand the return value back to the unit test proper. Here's the sort of thing I'm trying to do:

public static ObjectBuilder<User> BuildUserIn(this Fixture f, UserCollection uc)
{
   return f.Build<User>()
           .With(u => u.IsDeleted, false);
           .AfterCreation(u =>
            {
               var relation = f.Build<UserCollectionMembership>()
                               .With(ucm => ucm.UserCollectionId, uc.Id)
                               .With(ucm => ucm.UserId, u.Id)
                               .CreateAnonymous();
               Repository.Install(relation);
            }
}

Is something like this possible? Or perhaps there is a better way to accomplish my goal of creating an anonymous object graph?

2012-04-05 16:39
by ladenedge
Do you want to have a specific instance for the User type and reuse it's Id property value somewhere else - Nikos Baxevanis 2012-04-05 19:17
Does this help? http://stackoverflow.com/questions/5398258/customizing-autofixture-builder-with-seeded-property/5398653#539865 - Mark Seemann 2012-04-05 20:32
@MarkSeemann: Do() can apparently execute before the object is fully populated, so that doesn't work. I can (and do) manually execute the example lambda or do my object customization post-creation, but I'd love an alternative similar to the above - ladenedge 2012-04-09 07:31


6

For the Build method, this isn't possible, and probably never will be, because there are much better options available.

First of all, it should never be necessary to write static helper methods around the Build method. The Build method is for truly one-off initializations where one needs to define property or field values before the fact.

I.e. imagine a class like this:

public class MyClass
{
    private string txt;

    public string SomeWeirdText
    {
        get { return this.txt; }
        set
        {
            if (value != "bar")
                throw new ArgumentException();
            this.txt = value;
        }
    }
}

In this (contrived) example, a straight fixture.CreateAnonymous<MyClass> is going to throw because it's going to attempt to assign something other than "bar" to the property.

In a one-off scenario, one can use the Build method to escape this problem. One example is simply to set the value explicitly to "bar":

var mc =
    fixture.Build<MyClass>().With(x => x.SomeWeirdText, "bar").CreateAnonymous();

However, even easier would be to just omit that property:

var mc =
    fixture.Build<MyClass>().Without(x => x.SomeWeirdText).CreateAnonymous();

However, once you start wanting to do this repeatedly, there are better options. AutoFixture has a very sophisticated and customizable engine for defining how things get created.

As a start, one could start by moving the omission of the property into a customization, like this:

fixture.Customize<MyClass>(c => c.Without(x => x.SomeWeirdText));

Now, whenever the fixture creates an instance of MyClass, it's just going to skip that property altogether. You can still assign a value afterwards:

var mc = fixture.CreateAnonymous<MyClass>();
my.SomeWeirdText = "bar";

If you want something more sophisticated, you can implement a custom ISpecimenBuilder. If you want to run some custom code after the instance has been created, you can decorate your own ISpecimenBuilder with a Postprocessor and supply a delegate. That might look something like this:

fixture.Customizations.Add(
    new Postprocessor(yourCustomSpecimenBuilder, obj =>
        { */ do something to obj here */ }));

(BTW, are you still on AutoFixture 1.0? IIRC, there hasn't been an ObjectBuilder<T> around since then...)

2012-04-12 19:44
by Mark Seemann
Adding a postprocessor might be just the ticket, and I appreciate the advice on proper AF usage. Thank you! (Regarding v1.0, yes: we had some trouble upgrading - maybe the third time will be the charm! - ladenedge 2012-04-12 21:17


3

There's a useful discussion on this topic on the AutoFixture CodePlex site.

I believe my postprocessor Customization linked over there should help you. Example usage:

class AutoControllerDataAttribute : AutoDataAttribute
{  
    public AutoControllerDataAttribute()
        : this( new Fixture() )
    {
    }

    public AutoControllerDataAttribute( IFixture fixture )
        : base( fixture )
    {
        fixture.Customize( new AutoMoqCustomization() );
        fixture.Customize( new ApplyControllerContextCustomization() );
    }

    class ApplyControllerContextCustomization : PostProcessWhereIsACustomization<Controller>
    {
        public ApplyControllerContextCustomization()
            : base( PostProcess )
        {
        }

        static void PostProcess( Controller controller )
        {
            controller.FakeControllerContext();
            // etc. - add stuff you want to happen after the instance has been created
2012-07-25 20:14
by Ruben Bartelink
Ads