How to create anonymous test data with AutoFixture?

This article is the continuation of the previous article “Make your Unit Testing easier with AutoFixture”. In this article, we will cover how to create anonymous data and Objects with Autofixture.

If you also want to run the below examples on your machine make sure to install AutoFixture and XUnit Nuget Package. Also remember to use these two namespaces.

using Xunit;
using AutoFixture;

To create any anonymous data using Autofixture. You have to use the Fixture class, using an instance of the fixture class you can use Create method to provide any data type. Create<T>. AutoFixture will create anonymous data of that type.

Creating Anonymous Int

    [Fact]
    public void Ints()
    {
        //arrange 
        var sut = new IntCalculator();
        var fixture = new Fixture();
        var annonymousNumber = fixture.Create<int>();

        //act
        sut.Substract(annonymousNumber);

        //assert
        Assert.True(sut.Value < 0);
    }

Creating Anonymous String

In the below example you can see that we have provided another argument to the Create Method which is in the below case is “First_” and “Last_”. With that you Autofixure will prepend the given type, this is very helpful in case if your test fails and you need to know which property has caused the error. To provide this additional argument you have to install AutoFixture.SeedExtentions.

Please note: You only have to use this package in case of default data types like string, int, etc. For complex types, AutoFixture automatically handles this and prepends the property name.

public class StringDemo
{
    [Fact]
    public void Strings()
    {
        //arrange
        var str = new NameJoin();
        var fixture = new Fixture();
        var firstName = fixture.Create<string>("First_");
        var lastName = fixture.Create<string>("Last_");

        //act
        var result = str.Join(firstName, lastName);

        //asset
        Assert.Equal(firstName + ' ' + lastName, result);
    }
}

Creating Anonymous Instance of Complex Type

In the below example I have demonstrated two ways, one manual and the other using AutoFixture. As you can see with just one line of code AutoFixture handles all complex types and handle even nested class.

  [Fact]
        public void Manual()
        {
            //arrange
            Order order = new Order()
            {
                Id = 42,
                Customer = new Customer()
                {
                    CustomerName = "Akshay",
                    Id = 10
                },
                Items = new List<OrderItem>()
                {
                    {
                        new OrderItem
                        {
                            ProductName = "Product Name",
                            Quantity = 2
                        }
                    }
                }

            };
        }

        [Fact]
        public void UsingAutoFixure()
        {
            //arrange
            var fixture = new Fixture();
            Order order = fixture.Create<Order>();
            
            //act
        }

Creating Objects with Data Annotation

If you notice the Customer class we are using data annotation on property CustomerName with string length of 7. AutoFixture has support for numbers of data annotations and attribute. If you execute the code you will notice that AutoFixture will create the CustomerName with string length of 7.

using System.ComponentModel.DataAnnotations;    
public class Customer
    {
        public int Id { get; set; }

        [StringLength(7)]
        public string CustomerName{ get; set; }
    }
  [Fact]
        public void UsingAutoFixure()
        {
            //arrange
            var fixture = new Fixture();
            Order order = fixture.Create<Customer>();
            
            //act
        }

I hope you this will give you the idea to create anonymous data with AutoFixture. Stay tuned and subscribe for more article.
Happy Coding!

Leave a Comment

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