DEV Community

Cover image for Breathing Life into Your Test Data with the "Object Mother" Pattern
PeterMilovcik
PeterMilovcik

Posted on

Breathing Life into Your Test Data with the "Object Mother" Pattern

Hello there, fellow coding enthusiasts!

A test's best friend is its data. Without it, our tests would be but mere husks of potential. Yes, quality test data is crucial, and it's what we're diving into today! We're going to explore a neat little design pattern that's been making waves in the testing community: the "Object Mother".

Object Mother - The Fairy Godmother of Test Data

Picture this: You're working on a huge project, your test cases are multiplying faster than rabbits in spring, and each one needs a specific setup of objects. You find yourself copying and pasting boilerplate code just to create these objects. Sounds like a nightmare, right?

Enter the "Object Mother" pattern, your knight in shining armor (or rather, your fairy godmother). The idea behind the Object Mother pattern is devilishly simple: it's a helper that constructs ready-to-use test data.

A Practical Example: The User Object

Let's say you're working on an application where user management is crucial. You need User objects in many places, and creating them is tedious. Here's where an Object Mother comes to the rescue.

Let's create a UserMother class:

public static class UserMother
{
    public static User CompleteUser()
    {
        return new User
        {
            Name = "John Doe",
            Email = "john.doe@example.com",
            Password = "SuperSecurePassword123",
            Age = 25,
            Address = new Address
            {
                Street = "123 Elm St",
                City = "Springfield",
                State = "Unknown",
                ZipCode = "00000"
            }
        };
    }
}
Enter fullscreen mode Exit fullscreen mode

The CompleteUser() method of UserMother creates and returns a complete User object, initialized and ready for action. Your tests can now simply call UserMother.CompleteUser() to get a ready-to-use User object. Easy peasy!

Parameterizing your Mother

Of course, we don't always want the same data. Maybe you want a user of a specific age, or with a different email. Your fairy godmother has got you covered! Let's add some more methods to our UserMother:

public static class UserMother
{
    // ... CompleteUser() as before ...

    public static User UserWithAge(int age)
    {
        var user = CompleteUser();
        user.Age = age;
        return user;
    }

    public static User UserWithEmail(string email)
    {
        var user = CompleteUser();
        user.Email = email;
        return user;
    }
}
Enter fullscreen mode Exit fullscreen mode

Now you can call UserMother.UserWithAge(40) or UserMother.UserWithEmail("foo@bar.com") to get a User with specific attributes.

Summing it up

The Object Mother pattern helps us to keep our tests DRY and understandable. Instead of polluting our tests with setup code, we delegate object creation to a class that is specifically built for this purpose. It's like having your very own fairy godmother that ensures your tests are always invited to the ball, fully dressed and ready to dance!

It may seem like an extra step, but trust me, your future self will thank you for keeping the test data setup tidy and centralized. So why not give it a try in your next testing session?

In the meantime, happy coding, and remember - each line of code is a new adventure! Let me know in the comments how Object Mother has worked out for you. Stay tuned for more!

Top comments (0)