Skip to main content

Command Palette

Search for a command to run...

FactoryBot: Factory not registered (KeyError)

Updated
2 min read
FactoryBot: Factory not registered (KeyError)
A

I'm a passionate software developer.

Error

Factory not registered: "your_factory_name" (KeyError)
lib/active_support/hash_with_indifferent_access.rb:192:in `fetch': key not found: "your_factory_name" (KeyError)


Context

This error typically happens in the Rails console when you're using FactoryBot and try to build or create a factory that hasn't been defined, loaded or registered.


Solution

Require dependencies

Check if dependencies/libraries are already loaded

defined?(FactoryBot)
defined?(Faker)

If you get `nil`, you should require them.

irb(main):001> defined?(FactoryBot)

=> nil
irb(main):003> defined?(Faker)
=> nil
irb(main):004> require 'factory_bot'
=> true
irb(main):005> require 'faker'
=> true
irb(main):006> defined?(Faker)
=> "constant"
irb(main):007> require 'factory_bot'
=> false
irb(main):008>

Output

Check the factories

Let’s check if the factories really exist

FactoryBot.factories.map(&:name)
FactoryBot.find_definitions
FactoryBot.factories.map(&:name).include?(:your_factory_name)

OUtput

This command lists all registered factory names in FactoryBot as symbols (e.g., :user, :order). It’s useful for checking which factories are currently available, especially when debugging missing factory errors.

if you get an empty array, it’s because nothing is loaded or there are no factories.

  • FactoryBot.find_definitions

This tells FactoryBot to load all factory definitions from the default spec/factories (or test/factories) directory.
It's essential in environments like the Rails console, where factories aren’t auto-loaded like they are in test runs.

The output should be something like

["/srv/www/current/factories",
 "/srv/www/current/test/factories",
 "/srv/www/current/spec/factories"]

This checks if a specific factory (like :your_factory_name) is already registered and available in FactoryBot.

It’s a quick way to verify the presence of a factory before trying to build or create it, avoiding KeyError exceptions.

If you get true, then, you should celebrate because your factory is loaded and registered.


Test again

irb(main):019> FactoryBot.method(:build)
=> #<Method: #<Class:FactoryBot>(FactoryBot::Syntax::Methods)#build(name, *traits_and_overrides, &block) /srv/www/current/vendor/bundle/ruby/3.3.0/gems/factory_bot-6.2.1/lib/factory_bot/strategy_syntax_method_registrar.rb:27>
irb(main):020> FactoryBot.method(:create)
=> #<Method: #<Class:FactoryBot>(FactoryBot::Syntax::Methods)#create(name, *traits_and_overrides, &block) /srv/www/current/vendor/bundle/ruby/3.3.0/gems/factory_bot-6.2.1/lib/factory_bot/strategy_syntax_method_registrar.rb:27>
irb(main):021>

Output

Both method object for a given method if it's defined in the current scope:

  • Useful to inspect or call the method later

  • Raises a NameError if the method is undefined.


Done

The It Crowd Coffee GIF - Find & Share on GIPHY


Conclusion

When facing the error Factory not registered: "your_factory_name", it’s usually because the factory hasn’t been loaded, defined, or registered properly — especially in the Rails console, where automatic loading doesn't happen like it does during tests.

By systematically checking if FactoryBot and related dependencies are loaded (defined?), ensuring your factories are found (FactoryBot.find_definitions), and confirming your specific factory is present (FactoryBot.factories.map(&:name).include?(:your_factory_name)), you can quickly isolate and resolve the issue.

Finally, verifying method availability using FactoryBot.method(:build) or method(:create) gives you full confidence that FactoryBot is ready to use.


More from this blog

Alexandre Calaça's Blog

265 posts

I'm a passionate software engineer with a deep love for turning complex problems into elegant solutions through code.