[RUBY] An example of a FactoryBot [KeyError: Factory not registered: "user"] error

It's half unsolved, but it's a story of making a portfolio and doing something like this ... I got a title error when executing the unit test code of the model, but I did not find a similar case when I searched, so I wrote it. Briefly, I will only describe the conclusions and first aid.

Conclusion: There was a problem with the association (probably)

The instance was organized using FactoryBot, but an error occurred due to the following description. As a first aid, the error disappeared when I deleted it.

spec/factories/contents.rb


association :user

It's Factory Bot association. I wrote it because I want to pull the data in the users table. This isn't strange in itself, but I think it's because there's a problem writing another code and it's not working. We are investigating the cause, but if we can't do this, we can't test it, so I'd like to write it separately.

If you get the same error, you may want to doubt the above.

Postscript

I found out why the association didn't work. The bottom line is that the instance of spec / factories / user.rb was empty.

spec/factories/user.rb


FactoryBot.define do
  factory :user do

  end
end

For some reason, I just thought I had already written it. It's natural that it doesn't work. .. .. So I should have doubted that the association was able to write properly in the first place.

Recommended Posts

An example of a FactoryBot [KeyError: Factory not registered: "user"] error
Rails5 Rspec test error ArgumentError: Factory not registered: user
A simple example of an MVC model
An error occurred because only_full_group_by of sql_mode was not observed.