Getting factory_girl to work in Rails3
Asked Answered
S

3

7

For the life of me, I can't get factory_girl to work in Rails3. I created a brand new application. My Gemfile:

gem "rspec"
gem "rspec-rails"
gem "factory_girl_rails"

The generators in application.rb like so:

  config.generators do |g|
      g.test_framework :rspec, :fixture => true, :views => false, :fixture_replacement => :factory_girl
  end

Then, using the generator to create a new model:

> rails g model Addon name:string

  invoke  active_record
  create    db/migrate/20101223205918_create_addons.rb
  create    app/models/addon.rb
  invoke    rspec
  create      spec/models/addon_spec.rb
   error      factory_girl [not found]

What'd I miss? I did run bundle install of course... I tried looking around, but can't find any decent documentation on factory_girl and rails3.

Scone answered 23/12, 2010 at 21:8 Comment(0)
R
11

What you need is https://github.com/indirect/rails3-generators.

Rails 3 compatible generators for gems that don't have them yet...

The Factory Girl generators have moved to the factory_girl_rails gem...

Rooseveltroost answered 23/12, 2010 at 23:53 Comment(1)
Not needed with Rails 3.2 ... this is a bit out of date nowReactive
A
3

This is the proper approach in Rails 3.2 as of today:

Rails.application.config.generators do |g|
  g.test_framework :rspec, fixture: true
  g.fixture_replacement :factory_girl, dir: 'spec/factories'
end
A1 answered 30/8, 2012 at 13:33 Comment(1)
Just tried this with test/unit and it didn't work. Is this RSpec-only syntax? I used: g.test_framework :test_unit, fixture: true and g.fixture_replacement :factory_girlVallombrosa
A
2

Some users may be using factory_girl. Make sure that you are using factory_girl_rails.

Adrenocorticotropic answered 29/3, 2013 at 22:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.