Customizing a specific string inside a class using AutoFixture

Full source code.

I’ve been using AutoFixture for a while with my unit tests. It creates objects with prefilled data, saving me the hassle of manually constructing them.

Basic usage

If you want a string from AutoFixture do something like -

string myString = fixture.Create();
"c2eefff9-9cc2-4358-aee1-2d27b0476e41"

If you want to prefix the string do this -

string myPrefixedString = fixture.Create("Prefix");
"Prefix191dd4bc-f3ed-4d19-ac36-f3c84c958155"

If you want something that looks like an email address -

string emailAddress = fixture.Create().Address;
"9ed7e16b-f6df-42d6-8812-d7ea6580f300@example.org"

Where it starts to get a bit tricky is if you have a class like this -

public class Account
{
	public Guid AccountId { get; set; }
	public string Firstname { get; set; }
	public string Lastname { get; set; }
	public IEnumerable<string> EmailAddresses { get; set; }
}

Because I am using a string to represent an email address AutoFixture will give me a string, not an email address.

Here are two solutions.

Solution 1

fixture.Customize<Account>(c => c.With(a => a.EmailAddresses, fixture.CreateMany<MailAddress>().Select(ma => ma.Address.ToString())));
This produces emails that look like -

f45a37ae-6d2c-42a5-92ac-832e6ea2d028@example.net
72625222-e7af-4c29-96ed-4219efc1a859@example.net
54a39694-fd7a-458c-8739-0667ec9fa2d7@example.net

Solution 2

If I want to generate email address that look a little more like real world address I have to create a SpecimenBuilder.

 1    public class EmailAddressesStringSpecimenBuilder : ISpecimenBuilder
 2    {
 3        public object Create(object request, ISpecimenContext context)
 4        {
 5            var propertyInfo = request as PropertyInfo;
 6
 7            if (propertyInfo != null)
 8            {
 9                if (propertyInfo.Name == "EmailAddresses" && propertyInfo.PropertyType == typeof (IEnumerable<string>))
10                {
11					// of course you can customize how the mail addresses are created, you can even use a fixture to create strings :) 
12                    IEnumerable<string> emailAddresses = new List<string>() {"user1@acme.com", "user2@unknown.com", "user3@nothing.com"};
13                    return emailAddresses;
14                }
15            }
16            return new NoSpecimen();
17        }
18    }

How to use the customizations

1            Fixture fixture = new Fixture();
2
3			fixture.Customizations.Add(new EmailAddressesStringSpecimenBuilder());
4            // use one of the two customizations here, but not both
5			// fixture.Customize<Account>(c => c.With(a => a.EmailAddresses, fixture.CreateMany<MailAddress>().Select(ma => ma.Address.ToString())));
6
7            var account1 = fixture.Create<Account>();
8            var account2 = fixture.Create<Account>();
 
Full source code.

comments powered by Disqus

Related