Factory Girl uninitialized constant Model Name with rails-api
Asked Answered
C

1

1

I'm trying to make a factory for my User model, but when I run rspec, I get

Failure/Error: user = build(:user, email: "[email protected]")
 NameError:
   uninitialized constant User

When I hop into rails console, I can do User.all and it will return the users I have and FactoryGirl.build(:user, email: "[email protected]") works just fine, so I'm not sure where the problem is.

I've created the project with rails-api, using rails 4.2 ruby 2.2

# spec/spec_helper.rb
# i have to set spec/factories as the location explicitly or it won't find the factories

  require 'factory_girl_rails'
  RSpec.configure do |config|
    config.include FactoryGirl::Syntax::Methods

    FactoryGirl.definition_file_paths = [File.expand_path('../factories', __FILE__)]
    FactoryGirl.find_definitions
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
    end

    config.mock_with :rspec do |mocks|
      mocks.verify_partial_doubles = true
    end

    config.filter_run :focus
    config.run_all_when_everything_filtered = true

    if config.files_to_run.one?
      config.default_formatter = 'doc'
    end

    config.profile_examples = 10
    config.order = :random
  end
# spec/factories/user.rb
FactoryGirl.define do
  factory :user do
    email     "[email protected]"
    password  "password"
  end
end
#spec/requests/api/v1/sign_in_spec.rb
require 'spec_helper'

describe "Signing In" do

  it "will reject an invalid user" do
    user = build(:user, email: "[email protected]")
    post :new_api_v1_user_session, user: { email: user.email, password: user.password}
    is_expected.to respond_with 401
  end

end
#Gemfile
ruby '2.2.0'
gem 'rails', '4.2.0'
gem 'rails-api'
gem 'spring', :group => :development
gem 'pg'
gem 'active_model_serializers', '0.8.3'
gem 'devise'
gem 'simple_token_authentication'
gem 'rdoc'
gem 'responders'

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

Thanks!

Update: I can run FactoryGirl.build(:user) with rails console test without any issues...

Conspecific answered 17/2, 2015 at 22:9 Comment(3)
Can you try renaming the file to users.rb instead of user.rbHerrin
Also, because your factory girl syntax is correct, I assume factory girl isn't being included correctly. I don't use the following piece of config in my setup: FactoryGirl.definition_file_paths = [File.expand_path('../factories', __FILE__)] so I'm unsure why you need to define it, can you try removing it.Herrin
Hey, renaming the file to users does nothing. Also, I include the config because if I don't have that, I get the following on running rspec : ArgumentError: Factory not registered: userConspecific
C
7

Looks like all I needed to do was require 'rails_helper' in my spec_helper.rb. This also allowed me to remove the FactoryGirl.definition_file_paths from spec_helper.

Conspecific answered 17/2, 2015 at 22:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.