My Introduction To Factory Bot

Maria Cristina Simoes
4 min readFeb 20, 2019

--

I am a software engineering student at Flatiron School. While looking through my coursework, I noticed the use of factories in my curriculum and became curious what these were and why they kept appearing in my course labs. So, I did some research and here’s what I learned:

Factory Bot (previously known as Factory Girl) is a library gem specific to Ruby. It creates test fixtures, these test fixtures can be used as a tool to help with automated testing. Factory Bot is intended to be more dynamic than the default fixtures that Rails has.

With that, I googled — “What is a test fixture?”

“A test fixture is a fixed state of a set of objects used as a baseline for running tests. The purpose of a test fixture is to ensure that there is a well known and fixed environment in which tests are run so that results are repeatable.” (source: JUnit)

In other words — Factory Bot’s creates test fixtures that are fake test objects that can be re-used throughout testing an application.

Why is this useful? Factory Bot’s test fixtures can use fixed data that can be used to help create a fixed testing environment. It also, allows you to test out your models and code without touching your database. Factory Bot can still simulate a database very closely with different method features. Also, the ability to create objects efficiently in a controlled environment can also help testing for more scalable applications.

In addition, factories also allows testing for specific attribute information. For example, say we have a user and that has a name, age, address, school, and email. However, we want to just test the update of an email address. With Factory Bot, we can create a default “factory” that has default attributes besides the email address that we define in our test. This testing situation, reminds me of old school science fair projects! The factory helps to create a series of “control variables”, or in this case the default un-changed attributes of name, age, address and school. That way, you can create a factory object with only altering the “independent variable” or in this case the e-mail attribute.

Now let’s visualize this scenario with the situation below:

The first step is to define a factory. It is recommended to define one factory per class. For the User class, you can define a factory for our user, Harry Potter, like below.

Define a Factory:

name, age, address, school are the default values for the test. The email can be set to anything in the factory but let’s assume nil for the test.

Now let’s use this factory in a test. Imagine we want our test to play out this scenario: Harry entered his email to be thechosenone@hogwarts.com but since Hogwarts is a School of Witchcraft & Wizardry — the email should obviously end in .edu. Luckily, the software engineers for hogwarts.edu are basically magicians, so they are able to write a method that checks if his email is valid. We only want to test this email, so we can create a new user like below — and all the other attributes will be defaulted to the other attributes defined in the factory.

Use a Factory With a Test:

assumes that valid_email? is a method that checks the email & returns a boolean

Factory Bot comes with different methods create class instances. In this case we used create, create will automatically save the this user instance. Other methods include

build: Unlike create, this will not automatically save the instance

build_stubbed: creates a persisted-like instance. Simulates a database closely with a fake_id, created_at and any associations.

As a student, working with smaller sets of data, it took me a little bit to visualize the benefits of using factories. Below is a screenshot from ThoughtBot’s Factory Bot tutorial. In this example, you can see just how many attributes a user can have and why only defining one attribute to create this instance can be useful and efficient.

Where do these factories go? Factories will automatically load by calling the method below if saved in the files listed.

FactoryBot.find_definitions
test/factories.rb
spec/factories.rb
test/factories/*.rb
spec/factories/*.rb

There are a lot of other neat things you can do with Factory Bot. I would recommend checking out the first sources link below to learn more about set-up and the other ways you can utilize Factory Bot for testing in your applications.

--

--