RSpec and FactoryBot

This is aimed at anyone recently taking up RSpec testing for Ruby.

I am always looking for ways to refactor my code, a standard rule for programmers. But you can stare at code for so long and sometimes not see what you can change without potentially being exposed to a certain gem in someone else’s project or a commercial setting. Having self-taught myself RSpec I had no real guidance in what to learn, luckily there was one good Udemy course that is fairly basic but covers good practice, the core RSpec methods and the basic fundamentals of RSpec called: Testing Ruby With RSpec. I then bought the Effective Testing With RSpec 3 book. This book is a good reference point but I found the walkthrough Sinatra App at the beginning a little dense for someone new to code. I started building out a basic Recipe project to try to learn TDD for both the back end and front end.

So why do I mention FactoryBot in the title? Well, neither of the above resources mention this Gem. I found myself writing out instance_doubles, mocks, etc to be able to test my code. I found my code becoming fairly cluttered at the top to write all the necessary variables, but I understood it so it didn’t really matter much to me. But once I got into a commercial setting, I realised that I needed to make my code easy to pick up and readable for another developer. I originally thought I should be leaving tonnes of notes to describe what I was doing, but it was the complete opposite, I was meant to be writing code that was so concise and readable, that notes were barely even needed. My RSpec should have been reading like an English sentence, less so code. The commercial setting is where I found FactoryBot or FactoryGirl as it used to be called.

So what is FactoryBot? FactoryBot is a library that essentially allows you to build sample instances of models for testing, without having to write out a long let variable each time at the top of RSpec files.

To start, simply install the gem by adding it into your Gemfile under the development, test group.

Next, add this line into your rails helper file.

Create a factory folder in the spec folder and then add your model files you will be testing. So if you have a User model. Create a users.rb file in the factories folder and you are ready to create your factory instances, ready to be churned out on request. You may want to add the Faker Gem as well to create the necessary attributes in your factory.

Creating one is easy as shown below:

Here I have a user and a random user. So I have a user ready to create with specific defined attributes and a random user with Faker Gem generated ones.

Then to simply call these in my user_spec.rb or any other spec file.

This would build me an instance of my User class, that is not saved, if I wanted to save it then I can just change build to create.

You can also change the set attributes should you want to.

Would output Jane Doe as the subject.name.

There is so so much more to dig through with the FactoryBot Gem which can be read through here: FactoryBot. This includes building associations, traits, callbacks etc. I found that it really cleaned up my specs, made them far more readable, and allowed me to contain in a single place the generic instances I was using during testing. I’m surprised this Gem isn’t mentioned more in resources as I have found it so useful. You will very likely come across it in commercial code so the quicker you get used to how it all works the better!

Currently concentrating on Ruby!