2

Ruby on Railsチュートリアルに取り組んでいますが、FactoryGirlを使用してテストすると問題が発生します。これが私のエラー情報です:

/Users/user/Documents/code/RoR/test/sample_app/spec/factories.rb:13:in `block (2 levels) in <top (required)>': wrong number of arguments (1 for 0) (ArgumentError)
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/factory_girl-4.2.0/lib/factory_girl/syntax/default.rb:18:in `instance_eval'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/factory_girl-4.2.0/lib/factory_girl/syntax/default.rb:18:in `factory'
from /Users/user/Documents/code/RoR/test/sample_app/spec/factories.rb:12:in `block in <top (required)>'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/factory_girl-4.2.0/lib/factory_girl/syntax/default.rb:49:in `instance_eval'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/factory_girl-4.2.0/lib/factory_girl/syntax/default.rb:49:in `run'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/factory_girl-4.2.0/lib/factory_girl/syntax/default.rb:7:in `define'
from /Users/user/Documents/code/RoR/test/sample_app/spec/factories.rb:11:in `<top (required)>'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:245:in `load'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:245:in `block in load'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:236:in `load_dependency'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:245:in `load'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/factory_girl-4.2.0/lib/factory_girl/find_definitions.rb:16:in `block in find_definitions'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/factory_girl-4.2.0/lib/factory_girl/find_definitions.rb:15:in `each'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/factory_girl-4.2.0/lib/factory_girl/find_definitions.rb:15:in `find_definitions'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/factory_girl_rails-4.2.1/lib/factory_girl_rails/railtie.rb:33:in `block in <class:Railtie>'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/activesupport-3.2.12/lib/active_support/lazy_load_hooks.rb:34:in `call'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/activesupport-3.2.12/lib/active_support/lazy_load_hooks.rb:34:in `execute_hook'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/activesupport-3.2.12/lib/active_support/lazy_load_hooks.rb:43:in `block in run_load_hooks'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/activesupport-3.2.12/lib/active_support/lazy_load_hooks.rb:42:in `each'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/activesupport-3.2.12/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/railties-3.2.12/lib/rails/application/finisher.rb:59:in `block in <module:Finisher>'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/railties-3.2.12/lib/rails/initializable.rb:30:in `instance_exec'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/railties-3.2.12/lib/rails/initializable.rb:30:in `run'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/railties-3.2.12/lib/rails/initializable.rb:55:in `block in run_initializers'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/railties-3.2.12/lib/rails/initializable.rb:54:in `each'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/railties-3.2.12/lib/rails/initializable.rb:54:in `run_initializers'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/railties-3.2.12/lib/rails/application.rb:136:in `initialize!'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/railties-3.2.12/lib/rails/railtie/configurable.rb:30:in `method_missing'
from /Users/user/Documents/code/RoR/test/sample_app/config/environment.rb:5:in `<top (required)>'
from /Users/user/Documents/code/RoR/test/sample_app/spec/spec_helper.rb:51:in `require'
from /Users/user/Documents/code/RoR/test/sample_app/spec/spec_helper.rb:51:in `<top (required)>'
from /Users/user/Documents/code/RoR/test/sample_app/spec/controllers/pages_controller_spec.rb:1:in `require'
from /Users/user/Documents/code/RoR/test/sample_app/spec/controllers/pages_controller_spec.rb:1:in `<top (required)>'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/rspec-core-2.13.0/lib/rspec/core/configuration.rb:819:in `load'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/rspec-core-2.13.0/lib/rspec/core/configuration.rb:819:in `block in load_spec_files'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/rspec-core-2.13.0/lib/rspec/core/configuration.rb:819:in `each'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/rspec-core-2.13.0/lib/rspec/core/configuration.rb:819:in `load_spec_files'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/rspec-core-2.13.0/lib/rspec/core/command_line.rb:22:in `run'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/rspec-core-2.13.0/lib/rspec/core/runner.rb:80:in `run'
from /Users/user/.rvm/gems/ruby-1.9.3-p385/gems/rspec-core-2.13.0/lib/rspec/core/runner.rb:17:in `block in autorun'

そしてここに私のfactories.rbがあります:

FactoryGirl.define do
  factory :user do
    user.name "abc"
    user.email "abc@example.com"
    user.password "foobar"
    user.password_confirmation "foobar"
  end
end

これが私がuser_controller_spec.rbで使用するコードです:

before(:each) do
  @user = FactoryGirl.create(:user)
end

以前に尋ねた質問の方法を試しましたが、うまくいきませんでした。誰かが私を助けることができますか?ありがとう!

4

1 に答える 1

2

ファクトリファイルは次のようになります。

FactoryGirl.define do
  factory :user do
    name "abc"
    email "abc@example.com"
    password "foobar"
    password_confirmation "foobar"
  end
end

それはうまくいくでしょう。

初期バージョンではローカル変数「user」を使用しています。問題は、ブロックに渡された、またはブロック内に定義されたローカル変数ユーザーがないことです。上記のコメント投稿者の1人が指摘しているように、user.nameを呼び出したい場合は、|user|を渡す必要があります。ブロックに。すなわち

FactoryGirl.define do
  factory :user do |user|
  etc.

しかし、これはFactoryGirlでは必要ありません。上で入力したとおりに実行できます。

https://github.com/thoughtbot/factory_girl/wiki/Usageを読むことをお勧めします-チュートリアルでRailsを(私が行ったように)学習しているだけでも、非常にアクセスしやすくなっています:-)

于 2013-03-22T21:44:23.863 に答える