Constructing a personal rails assistant An introduction to FactoryBot!

Recently I have been learning Ruby on Rails or more commonly just simply known as Rails. For those that are unfamiliar, Rails is a framework used to build web applications. If we want to be more specific Rails is a software library that extends the Ruby programming language functionality. Due to the friendliness of the Ruby language, Rails has grown in popularity throughout the last couple of years. The large support allows rails to morph into whatever direction it needs to go(There is a Gem for everything).

Awesome! I am assuming that if you are reading this you are probably already using rails and know some basics, so let’s get to the juicy part. It all starts with TDD or Test Driven Development, In this case, we will be using RSpec which is a domain specific language that is written in ruby to test ruby code. In my developer upcoming I have been brought up to strictly practice TDD. In doing so, there have been many instances where I have been in need of making dummy data for all my testing. Depending on how your database is structured as far as relationships and the object that your database contains, this could become a very long time-consuming process.

Have no fear! We can use Factorybot!

Alright, so what is Factorybot? Factorybot, originally known as FactoryGirl, is a software library which is ruby specific, it was designed to provide factory methods(methods that have creational patterns) to quickly create objects. This can come in very handy primarily for testing and depending on the specific data needed, sometimes for your seeding. I view Factorybot as my testing personal assistant. In the timeframe that I have been using it, it has easily saved me twice the amount of time, it sure beats out creating each individual object for every needed test.

Lets get started, We are going to need the following gem placed under the (group :development, :test do) as shown below.

Gemfile

Awesome! let run Bundle install to obtain the Gem’s functionality. We will now create the folder structure that we will need in order to organize our FacoryBot helpers.

We have a few more steps before we can start creating objects. We will now have to insert this code

RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end

In the following path: spec/support/factory_bot.rb as pictured above. Finally, the last step, Uncomment the following code in our rails_helper.

Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

Let's get out Factorybot helper working! I am going to start you with the most basic way to create something.

FactoryBot.define do
factory :user do
name { "Stark" }
end
end

Awesome! We have successfully created our first Factorybot object. Everytime that we need a user we can use the code below.

user = create(:user)

We have now created a User with a name of “Stark”, If we wanted to add more attributes other than name to a user, we would simply follow the same format where the name is and continue adding more attributes. Okay so we can make users but they are all going to be the same, how can we make this dynamic? I am glad you asked. We can make things dynamic by doing the following code.

making things dynamic.

What is happening in the code above? Like the first factory we created, we are naming each specific attribute except this time around we add a sequence in front. If we observe the code that is after the attribute, we have |n| that is going to be a number incrementing by 1 everytime there is a new user created. That |n| is then placed in the string which also increases in the count, for example, the first user we make would have an email of (user_1@gmail.com), (Password 1) and so on. The second user would have (user_2@gmail.com), (Password 2). This allows our factories to create as many objects as we need and continue to be dynamic. If we wanted to increment the n value for each user, since we are using ruby, we can add or subtract from |n| variable. For example if we wanted to multiples of 10 for each |n| value, we would be able to do something like this.

FactoryBot.define do
factory :song do
sequence(:title) {|n| "Title #{n}" }
sequence(:length) {|n| n*10 }
play_count { 0 }
artist
end
end

The couple lines of code above save us from having to hardcoding all the needed objects. The code below has an example of what that would look like.

FactoryBot vs hardcode

If we look at the code above at first it might not seem to bad, but if we are going to have to make multiple users the keystrokes you save in the long run start adding up.

We have now seen how to make multiple Users with unique attributes using our Factorybot helper. The last thing that I am going to show you is how to make associations. Let's say we have to attach a User to a specific Item, the could below shows us how we would make the association using the Factorybot syntax.

Summary 
Overall there are many other useful ways that Factorybot can be implemented in Test Driven Development. This brief intro was designed to open the door to what Factorybot can offer. I hope this caught your interest, I will post more thorough resources below.

Sources: