Crank out Tests with Factory Bot and Faker

JP Lynch
5 min readJul 27, 2018

If you’re anything like me, you might love to code, but tests...well they aren’t as much fun. We all know how important they are, but there is a piece of you that simply wants to crank out the tests as fast as possible in order to spend more time on the actual project at hand. If this sounds like you, keep reading. If not, got write your tests. In this post, we’ll be examining the Factory Bot and Faker gems, and exploring the setup and offerings of each.

Factory Bot is a useful tool that allows us to call on a pre-made object with a short snippet within our tests. Combined with Faker, which allows us to randomize the object attributes based on a large internal library, we will be able to set up each test with minimal effort, and get back to coding our app as fast as possible. Setup is a breeze, and preparation is minimal compared to the time you spend creating objects by hand in each test file. The first step is to add ‘factory_bot_rails’ and ‘faker’ to our test group in the Gemfile. After this, make sure to bundle and bundle update so that the changes made here will take effect.

Gemfile setup

Factory bot then needs to set up with RSpec. We do that by creating a new folder/file structure in our spec folder, ‘support/factory_bot.rb’. Inside that new file, type in the configure do syntax shown below.

Incorporate Factory Bot with RSpec

The final piece of the puzzle for setting up Factory Bot, is to call upon this new ‘factory_bot.rb’ file inside our rails helper. We simply add the line “require ‘support/factory_bot’ ” as shown in the following screen snippet.

Add factory bot Rspec file into rails helper

We are now ready to start building our factories that will crunch out the objects we want made during our tests. Factory bot works by creating a ‘factory’ to produce each object, which is then called upon with a snippet of code in each test. These ‘factories’ will live in a new folder/file structure we create under spec, spec/factories/plural_model_name.rb, in our case it will be spec/factories/songs.rb. Inside the file, the factory will be made with the class name identified, along with the attributes.

Song factory file

The factory will always start with line 1, line 2 will identify the class that the factory is creating the object from, in this case we are creating from the class, Song. The ‘title’, ‘length’, and ‘play_count’ seen inside the factory are the attributes. When we call upon this factory in a test, it will create an instance of song with these attributes and the static values seen. Calling upon the factory in the test is as simple as create(:song), although this will generate an error for us because a song needs to be tied to an artist. So in this case, we would utilize the factory bot ability to inject custom attributes into the factory. This would looking like the following: create(:song, artist_id: Adele.id) where Adele is a previously created artist.

Example showing the Factory Bot method and the regular method for creating an instance of a class

Now we usually will want different/unique values for each instance attribute, and this is where Faker comes into play. We simply change the attribute syntax inside our factory. In order to create varied attributes for our song, we would set it up as so.

Faker used to produce a random song title

Faker is used here to produce a random title for each Song, although in this case, we had to use book titles for our song titles. Sometimes you need to get creative with the Faker categories in order to just get close enough to what you want. This is perfectly fine for testing purposes. For the length and play_count, you can simply pull random numbers. Faker has a long list of categories with methods you can call for each, listed on their GitHub page linked at the end of this post.

When utilized properly, Factory Bot can provide a quick means of producing numerous objects for all tests across an entire project without much typing. The more tests you create, the less typing overall that is required for each set up, and the more time you save for actual coding within your app. When combined with Faker, you can ensure that your objects are varied enough in order to properly produce proper test results.

There is a lot more depth involved with the Factory Bot and Faker Ruby gems, but this post was meant to simply get you acquainted with their functionalities and capabilities. For me information on these two gems, you can follow the links below to their GitHub repos.

Factory Bot: https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md

Faker: https://github.com/stympy/faker

--

--