All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

[Fix] Factory not registered: "user" (KeyError) (FactoryBot / Rails 7)

Post cover: [Fix] Factory not registered: "user" (KeyError) (FactoryBot / Rails 7)
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.

How does the artificial intelligence model work?
15 Sep 16:42

How does the artificial intelligence model work?

meme code
meme code@memecode
What does relational mean (in the context of relational databases)?
21 Sep 08:27

What does relational mean (in the context of relational databases)?

meme code
meme code@memecode
What is RDBMS (relational database management system)?
21 Sep 08:56

What is RDBMS (relational database management system)?

meme code
meme code@memecode
What does the ACID principle mean in programming?
24 Sep 20:31

What does the ACID principle mean in programming?

meme code
meme code@memecode
What is Convention over Configuration (in the context of RoR and beyond)?
26 Sep 07:47

What is Convention over Configuration (in the context of RoR and beyond)?

meme code
meme code@memecode
Rounding Decimal Numbers: Mathematical Rules, JavaScript, and Ruby
18 Oct 12:59

Rounding Decimal Numbers: Mathematical Rules, JavaScript, and Ruby

meme code
meme code@memecode
What is PORO in Ruby?
08 Dec 12:46

What is PORO in Ruby?

meme code
meme code@memecode
What is the difference between spec_helper.rb and rails_helper.rb in RSpec?
08 Dec 13:53

What is the difference between spec_helper.rb and rails_helper.rb in RSpec?

meme code
meme code@memecode
09 Dec 14:50

[Fix] No such file or directory @ rb_sysopen - tmp/pids/server.pid

meme code
meme code@memecode
What you need to know to build a successful freelance career in IT
23 Dec 16:03

What you need to know to build a successful freelance career in IT

meme code
meme code@memecode
Virtual cloud server: what it is and what makes it special
03 Jan 10:58

Virtual cloud server: what it is and what makes it special

meme code
meme code@memecode
[Fix] Heroku / SearchBox addon - indexing error "The client is unable to verify that the server is Elasticsearch"
31 Jan 13:09

[Fix] Heroku / SearchBox addon - indexing error "The client is unable to verify that the server is Elasticsearch"

meme code
meme code@memecode