ArgumentError: Factory not registered

邮差的信 提交于 2019-11-28 20:24:53

There seems to be an issue when using rspec / factory girl with spring.

Adding:

config.before(:all) do
  FactoryGirl.reload
end

in my spec_helper.rb solved the issue.

Credit: https://github.com/rails/spring/issues/88

Edit:

Another way to fix the issue is to manually tell Factory Girl where to load the factory. Add this in your spec_helper:

FactoryGirl.definition_file_paths = [File.expand_path('../factories', __FILE__)]
FactoryGirl.find_definitions

This seems to be an issue with Factory Bot. I fixed it (as per the issue report) with FactoryBot.find_definitions:

RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods

  config.before do
    FactoryBot.find_definitions
  end
end

This is not necessarily caused by Spring. There is an issue that means factory_girl loads the paths slightly different from rspec. The solution is to to add to the rails_helper the following

FactoryGirl.definition_file_paths << File.join(File.dirname(__FILE__), 'factories')
FactoryGirl.find_definitions

assuming the helper is at engine_root/spec.

This occurs when you are using rspec in a rails engine.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!