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...
FactoryGirl.definition_file_paths = [File.expand_path('../factories', __FILE__)]
so I'm unsure why you need to define it, can you try removing it. – Herrinrspec
:ArgumentError: Factory not registered: user
– Conspecific