views:

22

answers:

1

Hello.

I'm using Rails 3 / factory_girl_rails / Rspec 2 and Ruby 1.8

I've defined my factories this way:

Factory.define :user do |u|
  u.name       'Some guy'
  u.sequence(:email) {|n| "person#{n}@example.com" }
  u.password   'password'
end

Factory.define :password_reset_user, :parent => :user do |user|
  user.password_reset_key   '111222333444'
end

And my tests run fine.

I notice that on the factory girl page they define factories in their examples without passing a parameter after the do (http://github.com/thoughtbot/factory_girl):

# This will guess the User class
FactoryGirl.define :user do
  factory :user do
    first_name 'John'
    last_name  'Doe'
    admin false
  end

  # This will use the User class (Admin would have been guessed)
  factory :admin, :class => User do
    first_name 'Admin'
    last_name  'User'
    admin true
  end

  # The same, but using a string instead of class constant
  factory :admin, :class => 'user' do
    first_name 'Admin'
    last_name  'User'
    admin true
  end
end

In effort to clean up my code, I've tried to emulate defining factories without passing a parameter after the do and changed my code to:

Factory.define :user do
  factory :user do
    name       'Some guy'
    sequence(:email) {|n| "person#{n}@example.com" }
    password   'password'
  end

  factory :password_reset_user, :parent => :post do
    password_reset_key   '111222333444'    
  end
end

But I get the error:

undefined method `factory' for main:Object (NoMethodError)
    from /Library/Ruby/Gems/1.8/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:56:in `define'
    from /web/votropolis/spec/factories.rb:1
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:239:in `require'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:239:in `require'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:227:in `load_dependency'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:239:in `require'
    from /Library/Ruby/Gems/1.8/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:307:in `find_definitions'
    from /Library/Ruby/Gems/1.8/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:305:in `each'
    from /Library/Ruby/Gems/1.8/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:305:in `find_definitions'
    from /Library/Ruby/Gems/1.8/gems/factory_girl_rails-1.0/lib/factory_girl_rails/railtie.rb:11
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/lazy_load_hooks.rb:34:in `call'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/lazy_load_hooks.rb:34:in `execute_hook'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/lazy_load_hooks.rb:43:in `run_load_hooks'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/lazy_load_hooks.rb:42:in `each'
    from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.1/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/application/finisher.rb:46
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/initializable.rb:25:in `instance_exec'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/initializable.rb:25:in `run'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/initializable.rb:50:in `run_initializers'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/initializable.rb:49:in `each'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/initializable.rb:49:in `run_initializers'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/application.rb:134:in `initialize!'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/application.rb:77:in `send'
    from /Library/Ruby/Gems/1.8/gems/railties-3.0.1/lib/rails/application.rb:77:in `method_missing'
    from /web/votropolis/config/environment.rb:5
    from /web/votropolis/spec/spec_helper.rb:3:in `require'
    from /web/votropolis/spec/spec_helper.rb:3
    from /web/votropolis/spec/controllers/organizations/memberships_controller_spec.rb:1:in `require'
    from /web/votropolis/spec/controllers/organizations/memberships_controller_spec.rb:1
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.0.1/lib/rspec/core/configuration.rb:306:in `load'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.0.1/lib/rspec/core/configuration.rb:306:in `load_spec_files'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.0.1/lib/rspec/core/configuration.rb:306:in `map'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.0.1/lib/rspec/core/configuration.rb:306:in `load_spec_files'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.0.1/lib/rspec/core/command_line.rb:18:in `run'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.0.1/lib/rspec/core/runner.rb:55:in `run_in_process'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.0.1/lib/rspec/core/runner.rb:46:in `run'
    from /Library/Ruby/Gems/1.8/gems/rspec-core-2.0.1/lib/rspec/core/runner.rb:10:in `autorun'
    from /Library/Ruby/Gems/1.8/bin/rspec:19

What am I doing wrong? Or is it a compatibility issue -- i.e. perhaps factory_girl_rails (which is for rails 3) doesn't work the same way as factory_girl).

Thanks.

Sean

A: 

It's because this documentation is only to FactoryGirl beta. Not stable version. It's explain on top of README.

The stable documentation is here : http://github.com/thoughtbot/factory_girl/tree/1.3.x

shingara