3 Useful AutoFixture Features to Level Up Your .NET Testing Game

AutoFixture is a testing library for .NET applications. It allows you to automatically create objects that you can use in your tests. It also simplifies the testing process and makes tests more resilient to refactors. It’s simple enough to pick up but has plenty of features that I hadn’t learned about until I had been using it for a while. Here are a few cool things AutoFixture can do that may come in handy.

Ensure the same object instance gets generated every time.

Sometimes we want to ensure that whenever AutoFixture generates an object, it uses the same instance every time. Maybe we have a Person class like this:


public class Person
{
    public string Name { get; set; }
}

And a Job class like this:


public class Job
{
    public Person JobHolder { get; set; }
}

We want to ensure that wherever in our test we generate a Job, it uses the exact same Person instance instead of generating a new one each time.

Using the freeze() function, we can do exactly that just like this:


AutoFixture.Freeze<Person>();

Now, every subsequent time we do the following:


AutoFixture.Create<Job>();

We will have the same Person instance as a value of every Job AutoFixture generates for us.

Call a function on the object before it’s created with Do().

Say you have a class that needs to have some method of it called before you can test with it. As an example, let’s say we have a Car class that looks like this:


public class Car
{
    int Gas = 0;

    public void FillTank() {
        this.Gas = 100;
    }

    public void Run() {
        if (this.Gas == 0) {
            Console.WriteLine("Out of gas!");
            return;
        }
        this.Gas = this.Gas - 10;
    }
}

We want to write a test that requires us to call the Run() function. To do that, we need to ensure that FillTank() gets called first. We can do that using the Do() function, like this:


var car = AutoFixture.Build<Car>().Do(x => x.fillTank()).Create();

Now on creation, our car object has a full tank of gas and is ready for testing.

Use customizations to change how some values get generated.

Sometimes we still want AutoFixture to randomly generate some values for us, but we want them to be generated in a specific way. Customizations are a great way to do this.

Reusing our Person class from earlier, we want to make it so that, every time a Person class is generated, their Name is always “Joe.”

First, we’ll create a customization class like this:


public class PersonsNameIsJoe : ICustomization
{
    public void Customize(IFixture fixture)
    {
        fixture.Customize(composer => composer.With(p => p.Name, "Joe"));
    }
}

Next, we set up our customizer:


AutoFixture.Customize(new PersonsNameIsJoe());

Now any time we do this:


var person = AutoFixture.Create<Person>();

Our Person instance will have the name “Joe”.

Get More Out of AutoFixture

The fun doesn’t have to end here! AutoFixture is an incredibly comprehensive testing library, and I highly recommend poking around the docs for other features that could be useful. Generally, whenever you run into an issue where you’re wondering “can AutoFixture do this?”, the answer is going to be yes.

Conversation

Join the conversation

Your email address will not be published. Required fields are marked *