Rails 3: uninitialized constant FactoryGirl
Asked Answered
L

1

7

I am trying to create my first controller test using FactoryGirl for my Rails application, but I keep retrieving the following error:

uninitialized constant FactoryGirl

My Factories.rb file looks like this:

FactoryGirl.define do
  factory :offer, class: "Offer" do |f|
    f.title     "Some title"
    f.description   "SomeDescription"
  end
end

And my controller test looks like this:

require 'spec_helper'

describe OffersController do
 def valid_session
    {}
  end

  describe "GET index" do
     before { 
        @offer = FactoryGirl.create(:offer)
     }

    it "assigns all offers as @offers" do    
      get :index, {}, valid_session
      assigns(:offers).should eq([@offer])
    end
  end
end

My Gemfile looks like this:

group :development, :test do
  gem 'sqlite3'
  gem 'rspec-rails'
  gem 'capybara', '1.1.2'
  gem 'factory_girl_rails', '>= 4.1.0', :require => false
end

What might I be missing since FactoryGirl isn't present?

Lauds answered 8/12, 2012 at 20:14 Comment(0)
V
19

You propably forgotten to require factory_girl in spec_helper.rb.

Veradia answered 8/12, 2012 at 20:47 Comment(3)
Such a simple mistake. Thanks for the help.Lauds
I suggest you to remove :require => false. Then you can use FactoryGirl also in rails console and can generate models using rails generate factory_girl:model.Veradia
when the gem 'factory_girl_rails' is install it also installs 'factory_girl' as a dependancy. So why must it be included in this file? Your suggestion worked for me, I'm just curious to know what the heck this is so! Thanks!Sporocyst

© 2022 - 2024 — McMap. All rights reserved.