我正在研究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'
这是我的个工厂.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
我在以前的问题中尝试了这些方法,但它们没有用。任何人都可以帮我吗?谢谢!
答案 0 :(得分:2)
您的工厂文件应该是:
FactoryGirl.define do
factory :user do
name "abc"
email "abc@example.com"
password "foobar"
password_confirmation "foobar"
end
end
那会有用。
您在初始版本中使用了本地变量'user'。问题是没有局部变量用户传递给或在块中定义。正如上面的一位评论者指出的那样 - 如果你想调用user.name,那么你必须传递| user |到街区。即。
FactoryGirl.define do
factory :user do |user|
etc.
但在FactoryGirl中没有必要这样做。你可以在上面输入它时这样做。
我建议阅读https://github.com/thoughtbot/factory_girl/wiki/Usage - 即使您只是通过教程学习Rails(就像我一样),它也非常易于访问: - )