# FactoryBot: Factory not registered (KeyError)

## Error

```ruby
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)
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1743444850344/efe8161a-66fa-4276-be48-cccaccb2618a.png align="center")

---

### 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

```ruby
defined?(FactoryBot)
defined?(Faker)
```

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

```ruby
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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1743445315507/e9f2bdd1-93f4-4af0-bf43-8c9342d816fe.png align="center")

### Check the factories

Let’s check if the factories really exist

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

OUtput

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1743445549604/2df349cb-2d4d-493b-a79f-9f4350e334f2.png align="center")

* [`FactoryBot.factories.map`](http://FactoryBot.factories.map)`(&:name)`
    

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

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

* ### [`FactoryBot.factories.map`](http://FactoryBot.factories.map)`(&:name).include?(:your_factory_name)`
    

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

```ruby
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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1743446199360/4c1f3f2a-6a04-4c08-a8c9-518ecce90b74.png align="center")

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](https://media3.giphy.com/media/1nDP1Mv2eIATu/200.gif?cid=6c09b952ppn5v297zr9wxhumzrrehqtfxa6t267xk5715j3p&ep=v1_internal_gif_by_id&rid=200.gif&ct=g align="left")

---

### 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`](http://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.

---
