ShaneKm
ShaneKm

Reputation: 21368

AutoFixture AutoData with arguments

How can you specify arguments for AutoData?

For example I have the following in my code:

var fixture = new Fixture();
fixture.Customizations.Add(
   new TypeRelay(
      typeof (IOrder),
      typeof (Order)));

I would like to have a test run several times with different fixture data. How would I set stuff like:

.Customizations.Add() or .Build()/.With/.Do so that when using AutoData fixture has this specified?

Upvotes: 2

Views: 1576

Answers (1)

Mark Seemann
Mark Seemann

Reputation: 233377

You can package various repeated AutoFixture customizations into one or more Customizations.

For the particular example, it'd look like this:

public class OrderCustomization : ICustomization
{
    public void Customize(IFixture fixture)
    {
        fixture.Customizations.Add(
            new TypeRelay(
                typeof(IOrder),
                typeof(Order)));
    }
}

Usage:

var fixture = new Fixture().Customize(new OrderCustomization());
var order = fixture.Create<IOrder>();

You can use them with [AutoData] by creating a derived attribute that passes a Fixture instance to the appropriate base class constructor.

Upvotes: 4

Related Questions