2017-10-12 100 views
0

因爲我發現這是一個非常普遍的問題,但我仍然無法解決它。參數錯誤:工廠未註冊 - Rails 5

我創建了一個用戶工廠(我用的設計),在規格/工廠/ users.rb的:

FactoryGirl.define do 
    factory :user do 
    first_name "Jonas" 
    last_name "Sniegas" 
    email "[email protected]" 
    password "asdfasdf" 
    password_confirmation "asdfasdf" 
    end 
end 

並嘗試從測試控制檯創建它(rails c -e test):

FactoryGirl.create(:user)

然後:

ArgumentError: Factory not registered: user from /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' from /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 run' from /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 console' from /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 ' 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/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:293:in block in require' from /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' 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/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 `require'

的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' 

我也試圖在規格/ rails_helper.rb的before do塊添加FactoryGirl.find_definitions,並指定在一個文件中的所有工廠 - 規格/ factories.rb。似乎沒有什麼工作..

對此有什麼幫助?

回答

0

問題發生在春天...不得不阻止它 - spring stop

現在一切正常。我會留下這個答案,因爲在我遇到的很多問題和答案中,沒有人建議停止預加載器...

+1

當從命令行運行rspec時,還可以將'DISABLE_SPRING = true'與'bundle exec rspec'來抑制它再次啓動。 – seanriordan08

+1

不幸的是,您需要在排除故障的過程中儘早添加「發生奇怪的事情時,重新啓動彈簧」。 –

相關問題