DEV Community

Lucas Barret
Lucas Barret

Posted on

Automating Database Population in Rails Testing with Runners and FactoryBot

TLDR

To populate your Rails database with test data, for manual testing using FactoryBot, create a Ruby file and add :

FactoryBot.create(:coffee)
Enter fullscreen mode Exit fullscreen mode

Assuming you have a coffee model and a coffee factory. This will create a new record in the database with the values defined in your factory. You can also create a new factory specifically for this purpose if needed.
Finally, run the Ruby file using :

rails runner path/to/my/runner
Enter fullscreen mode Exit fullscreen mode

This will execute the code in your Rails application context and populate your database.

Introduction

When you need to populate your database for manual testing, using Rails runners and FactoryBot can save you a lot of time and effort. In this article, I will explain how I use runners and FactoryBot to create test data and populate my database in my development env.

Runners

Runners is part of rails CLI, it enables you to run code in you rails application context. Runners loads the Rails environment and then runs the specified Ruby code. This allows you to access your Rails models, controllers, and other application code from a standalone Ruby script.

This means you can easily run scripts that interact with your application's data, or perform complex tasks that require access to your application's functionality.

FactoryBot

FactoryBot is a Ruby gem that provides simple and flexible way to create test data for a Ruby or Rails application.
This gem is maintained by Toughbot, inc.

I will not dig deep here on how to use FactoryBot, since I aim to make a comprehensive guide of it soon in another article.
But here's a example, let's say you have a Coffee model with name and origin as attributes.

Factory.define do
  factory :coffee do
    name { "Guji" }
    origin { "Ethiopia" }
  end
end
Enter fullscreen mode Exit fullscreen mode

Populating database with FactoryBot

Create a ruby file in your tmp directory in this example I'll call it my_runner.rb.

Then in the script of your runner you can add the following lines :

FactoryBot.create(:coffee)
Enter fullscreen mode Exit fullscreen mode

Then in your terminal execute your runner with the rails runner cli you can run your ruby code in your rails context.

rails runner /path/to/my_runner.rb
Enter fullscreen mode Exit fullscreen mode

After at the end of the runner, your database will be populated with a new coffee row with the name "Guji" and the origin "Ethiopia".

Conclusion

In conclusion, using Rails runners and FactoryBot can be a great way to save time and effort when you need to populate your database for manual testing.
By using runners, you can easily run scripts that interact with your application's data, while FactoryBot enables you to create test data quickly and easily.
So, give it a try and see how runners and FactoryBot can make your manual testing faster and more efficient.

Top comments (2)

Collapse
 
antooooony profile image
anto

Bon article, les test sont trop souvent sous estimé !

Collapse
 
yet_anotherdev profile image
Lucas Barret

Thank you, I totally agree with you !
Testing is a really cool stuff to do if you have the right tool like RSpec 😇