ArgumentError:工厂未注册 - Rails 5

时间:2017-10-12 13:53:00

标签: ruby-on-rails rspec ruby-on-rails-5 factory-bot

正如我发现这是一个非常普遍的问题,但我仍然无法解决它。

我在spec / factories / users.rb中创建了一个用户工厂(我使用Devise):

FactoryGirl.define do
  factory :user do
    first_name "Jonas"
    last_name "Sniegas"
    email "test@test.com"
    password "asdfasdf"
    password_confirmation "asdfasdf"
  end
end

尝试从测试控制台(rails c -e test)创建它:

FactoryGirl.create(:user)

然后:

  

ArgumentError:工厂未注册:用户来自   /Users/user/.rvm/gems/ruby-2.3.0/gems/factory_girl-4.7.0/lib/factory_girl/registry.rb:24:in   find' from /Users/user/.rvm/gems/ruby-2.3.0/gems/factory_girl-4.7.0/lib/factory_girl/decorator.rb:10:in method_missing'来自   /Users/user/.rvm/gems/ruby-2.3.0/gems/factory_girl-4.7.0/lib/factory_girl.rb:85:in   factory_by_name' from /Users/user/.rvm/gems/ruby-2.3.0/gems/factory_girl-4.7.0/lib/factory_girl/factory_runner.rb:12:in从'跑'   /Users/user/.rvm/gems/ruby-2.3.0/gems/factory_girl-4.7.0/lib/factory_girl/strategy_syntax_method_registrar.rb:20:in block in define_singular_strategy_method' from (irb):1 from /Users/user/.rvm/gems/ruby-2.3.0/gems/railties-5.0.6/lib/rails/commands/console.rb:65:in start'from   /Users/user/.rvm/gems/ruby-2.3.0/gems/railties-5.0.6/lib/rails/commands/console_helper.rb:9:in   来自start' from /Users/user/.rvm/gems/ruby-2.3.0/gems/railties-5.0.6/lib/rails/commands/commands_tasks.rb:78:in 控制台'   /Users/user/.rvm/gems/ruby-2.3.0/gems/railties-5.0.6/lib/rails/commands/commands_tasks.rb:49:in   来自run_command!' from /Users/user/.rvm/gems/ruby-2.3.0/gems/railties-5.0.6/lib/rails/commands.rb:18:in '   /Users/user/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:293:in require' from /Users/user/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:293:in阻止在'require'中   /Users/user/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:259:in load_dependency' from /Users/user/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:293:in require'from / Users / user / Desktop / Ruby / Test / bin / rails:9:in <top (required)>' from /Users/user/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:287:in load'from   /Users/user/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:287:in block in load' from /Users/user/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:259:in load_dependency'来自   /Users/user/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:287:in load' from /Users/user/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in require'from   /Users/user/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in'requirement'

的Gemfile:

source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end

gem 'rails', '~> 5.0.3'
gem 'pg', '~> 0.18'
gem 'puma', '~> 3.0'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.2'

gem 'jquery-rails'
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.5'
gem 'sdoc', '~> 0.4.0', group: :doc


group :development, :test do
  gem 'byebug', platform: :mri
  gem 'rspec-rails', '~> 3.0'
  gem 'factory_girl_rails'
  gem 'capybara'
  gem 'database_cleaner'
end

group :development do

  gem 'web-console', '>= 3.3.0'
  gem 'listen', '~> 3.0.5'

  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

gem 'devise', '~> 4.2'
gem 'bootstrap-sass', '~> 3.3', '>= 3.3.6'

我还尝试在spec / rails_helper.rb的FactoryGirl.find_definitions块中添加before do,并在一个文件中指定所有工厂 - spec / factories.rb。似乎没有什么工作..

对此有何帮助?

1 个答案:

答案 0 :(得分:1)

问题出在春天......不得不阻止它 - spring stop

现在一切顺利。我会留下这个答案,因为在我经历过的许多问题和答案中,没有人建议停止预加载......