Search code examples
ruby-on-rails-3.2factory-botrspec-rails

factory_girl_rails 4.1.0 in rails 3.2 environment produces 'Factory not registered: user'


I am a factory_girl noob and have not been able to determine why the Factory is not 'registered'. Not sure where it is registered.

relevant portion of Gemfile:

...
group :development, :test do
  gem 'rspec-rails'
  gem 'factory_girl_rails'
end

group :test do
  gem 'faker'
  gem 'capybara'
  gem 'guard-rspec'
  gem 'launchy'
end
...

relevant portion of config/application.rb:

...
module Appname
  class Application < Rails::Application
    ...
    config.generators do |g|
      g.test_framework :rspec,
        :fixtures => true,
        :view_specs => false,
        :helper_specs => false,
        :routing_specs => true,
        :controller_specs => true,
        :request_specs => true
      g.fixture_replacement :factory_girl, :dir => "spec/factories"
    end
    ...
  end
end

relevant portion of spec/models/user_spec.rb:

require 'spec_helper'

describe User do
  it "has a valid factory" do
    FactoryGirl.create(:user).should be_valid
  end
  ...
end

result of $rspec spec/models/user_spec.rb:

Failures:

  1) User has a valid factory
     Failure/Error: FactoryGirl.create(:user).should be_valid
     ArgumentError:
       Factory not registered: user

UPDATE:

spec/factories/users.rb:

require 'faker'

FactoryGirl.define do
  factory :contact do |f|
    f.first_name { Faker::Name.first_name }
    f.last_name { Faker::Name.last_name }
    f.email { Faker::Internet.email }
    f.password { Faker::Name.last_name }
    f.password_confirmation { Faker::Name.last_name }
  end
end

Thanks for your help.


Solution

  • From the comments thread: you probably want to change factory :contact in your factory to factory :user:

    FactoryGirl.define do
      factory :user do |f|
        ...
      end
    end