Question

I was hoping somebody would be able to help me with a problem I am having with an uninitialized constant error I am receiving when attempting to use factory_girl. I know similar questions have been posted on stack overflow, and I have attempted to find a solution within these posts but to no avail.

I have a non rails project which I am using rspec to test. I wish to use factory_girl to create test data.

So I have my factory defined in a file called users.rb in the spec/factories folder. Factory is as follows:

FactoryGirl.define do
   factory :user do
   email 'root@test.com'
   password 'TestPass'
 end

end

I then have a test:

require 'spec_helper'

describe "login tests" do

  it "should allow me to login" do
    visit('http://mytestapp.com')
    user = FactoryGirl.build(:user)
    login(user.email, user.password)
    should have_content('Welcome!')
  end

end

In my spec helper I have

require 'factory_girl'
require_rel('factories')

RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods
end

However when I run my test I receive the error message "uninitialized constant User"

Please note in the spec_helper file I use the gem reqiure_rel to find the factories folder. I tried using FactoryGirl.find_definitions but when I used this I got the error message "Factory not registered: user". So using require_rel it appears the factory is at least found. In my test file I have also tried using this:

user = FactoryGirl.create(:user)

but I get the same error message.

Has anybody got any idea what I am doing wrong? Help would be greatly appreciated!!

Thanks!

Était-ce utile?

La solution

Probably it is not the issue with FactoryGirl. RSpec doesn't see your User class. So you should require the file with User class. And it will be better to describe User not login tests.

Autres conseils

I believe that FactoryGirl relies on the rspec-rails gem to locate and autoload models when the associated constants are referenced (e.g. loading .../app/models/user.rb when User is referenced). If you wanted to operate without that gem, you'd have to enable that autoloading yourself.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top