因爲我發現這是一個非常普遍的問題,但我仍然無法解決它。參數錯誤:工廠未註冊 - 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:infactory_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:inblock 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:instart' 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:inrun_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:inrequire' 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:inload_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:inblock 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:inload' 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。似乎沒有什麼工作..
對此有什麼幫助?
當從命令行運行rspec時,還可以將'DISABLE_SPRING = true'與'bundle exec rspec'來抑制它再次啓動。 – seanriordan08
不幸的是,您需要在排除故障的過程中儘早添加「發生奇怪的事情時,重新啓動彈簧」。 –