Reputation: 357
I'm working on the ruby on rails tutorial, but I encounter problems when testing using FactoryGirl. here is my error information:
/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'
and here is my factories.rb:
FactoryGirl.define do
factory :user do
user.name "abc"
user.email "[email protected]"
user.password "foobar"
user.password_confirmation "foobar"
end
end
here is the code I use in user_controller_spec.rb:
before(:each) do
@user = FactoryGirl.create(:user)
end
I've tried the methods in previous asked questions, but they did not work. Can anybody help with me? Thanks!
Upvotes: 2
Views: 2345
Reputation: 8202
Your factory file should just be:
FactoryGirl.define do
factory :user do
name "abc"
email "[email protected]"
password "foobar"
password_confirmation "foobar"
end
end
That'll work.
You are using a local variable 'user' in your initial version. Problem is that there has been no local variable user passed to, or defined in the block. As one of the commenters above points out - if you want to call user.name then you have to pass |user| to the block. i.e.
FactoryGirl.define do
factory :user do |user|
etc.
But this is not necessary in FactoryGirl. You can just do it as I typed it above.
I'd recommend reading https://github.com/thoughtbot/factory_girl/wiki/Usage - it's very accessible even if you're just learning Rails (as I did) via the tutorial :-)
Upvotes: 2