Table of contentsClick link to navigate to the desired location
This content has been automatically translated from Ukrainian.
When setting up tests in a new project with several models - User, Post, and so on. All factories were registered normally, except for User.
Loading development environment (Rails 7.0.4.3) irb(main):001:0> FactoryBot.create(:user) /Users/memecode/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/activesupport-7.0.4.3/lib/active_support/hash_with_indifferent_access.rb:194:in `fetch': Factory not registered: "user" (KeyError) /Users/memecode/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/activesupport-7.0.4.3/lib/active_support/hash_with_indifferent_access.rb:194:in `fetch': key not found: "user" (KeyError)
Calling FactoryBot.factories also confirmed that all factories except User were registered. After searching for the reason, it turned out that the FactoryBot library was simply installed, but we need FactoryBot for Rails.
Fixing the factory registration error (Factory not registered: "user")
So we change:
gem 'factory_bot', '~> 6.5'
to
gem 'factory_bot_rails', '~> 6.4', '>= 6.4.4'
Of course, we run
bundle
And everything will work. factory_bot_rails has factory_bot as a dependency, so we only need the library in our Gemfile.
What is the difference between factory_bot and factory_bot_rails?
factory_bot is the core library for working with factories, a kind of "simple Ruby tool". When you need a straightforward and reliable way to create test data in Ruby, factory_bot takes on this task.
It allows you to define factories that create models with convenient settings. For example, do you want to generate a user? Instead of manually specifying all the parameters, factory_bot will automatically fill them in for you. But there is one condition: you must take care of how these factories will be loaded in your project, especially if it is something more complex than a standard Rails application.
And what about factory_bot_rails? It is more of an "assistant" designed to make working with factory_bot in Rails as seamless as possible.
Imagine you have a large Rails application with several dozen models and tests. Instead of manually setting up the factory connections each time, factory_bot_rails does this automatically. It integrates with the Rails testing environment and ensures that all factories are ready to use. Moreover, it adds several useful commands in Rails, for example, for quickly creating factories for new models.
So, it can be said that factory_bot is the foundation, and factory_bot_rails is a wrapper that makes this foundation convenient specifically for Rails developers. They work together: the first provides functionality, while the second offers comfort. If you have a Rails application, you only need factory_bot_rails, as it already contains everything necessary. But if you want to use factories outside of Rails or customize them for your needs, then you will need factory_bot.
And it's worth mentioning the Faker library, which is very often used to generate content in factories. It will save time and allow you to create objects that are more similar to real ones.
This post doesn't have any additions from the author yet.