0
votes

I am using rspec-rails, factory_girl_rails and mogoid_rspec gems. After adding factory girl gem, I keep getting the error Factory not registered: user (ArgumentError) for my user factory. Following are the related code snippets:

In my Gemfile:

group :development, :test do
  gem 'byebug'
  gem 'rspec-rails'
  gem 'mongoid-rspec', '~> 2.1.0'
end

group :test do
  gem 'database_cleaner'
  gem 'faker'
  gem 'shoulda-matchers'
  gem 'factory_girl_rails'
end

rails_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)

# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'factory_girl_rails'
FactoryGirl.definition_file_paths = [File.expand_path('../factories', __FILE__)]

FactoryGirl.find_definitions
require 'support/mongoid'
require 'support/factory_girl'
require 'support/disable_active_record_fixtures'
require 'mongoid-rspec'

ENV["RAILS_ENV"] ||= 'test'


RSpec.configure do |config|
  config.before(:all) do
    FactoryGirl.reload
  end

  config.include Mongoid::Matchers, type: :model

  config.before(:suite) do
    DatabaseCleaner[:mongoid].strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner[:mongoid].start
  end

  config.after(:each) do
    DatabaseCleaner[:mongoid].clean
  end

  config.use_transactional_fixtures = false

  config.infer_spec_type_from_file_location!
end

factories/user.rb

FactoryGirl.define do
  factory :user do
    first_name     Faker::Name.first_name
    last_name      Faker::Name.last_name
    email          Faker::Internet.email
  end
end

spec/support/factory_girl.rb

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

spec_helper.rb

RSpec.configure do |config|

  config.expect_with :rspec do |expectations|

    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

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

Any help would be much appreciated. Thanks in advance! :)

1
I don't know if that's a typo, but your factory filename should be plural (factories/users.rb) instead of singular (factories/user.rb). Check if that helps. - ZuzannaSt
@ZuzannaSt, thanks but still the same error :( - Radhika
maybe you could config your factories paths the other way, placing this line inside your Rspec.configure do |config|: config.fixture_path = "#{::Rails.root}/spec/fixtures" - ZuzannaSt
@ZuzannaSt - Nope, doesn't work. - Radhika
could you add your spec_helper.rb? - ZuzannaSt

1 Answers

0
votes

As we've discussed, I'm pasting my proposition of what you could change in your code (including good practices).

  1. Define factory_girl_rails gem inside group :test, :development.

    #Gemfile
    group :development, :test do
      gem 'byebug'
      gem 'rspec-rails'
      gem 'factory_girl_rails'
      gem 'faker'
      gem 'mongoid-rspec', '~> 2.1.0'
    end
    
    group :test do
      gem 'database_cleaner'
      gem 'shoulda-matchers'
    end
    
  2. Require your support files in one single line instead of multiply definitions.

    # spec/rails_helper.rb 
    ENV['RAILS_ENV'] ||= 'test' 
    require 'spec_helper' 
    require File.expand_path('../../config/environment', __FILE__) 
    require 'rspec/rails'
    
    # Prevent database truncation if the environment is production 
    abort("The Rails environment is running in production mode!") if Rails.env.production?
    
    Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
    
    RSpec.configure do |config|   
      config.before(:all) do
        FactoryGirl.reload   
      end
    
      config.include Mongoid::Matchers, type: :model
    
      config.before(:suite) do
        DatabaseCleaner[:mongoid].strategy = :truncation   
      end
    
      config.before(:each) do
        DatabaseCleaner[:mongoid].start   
      end
    
      config.after(:each) do
        DatabaseCleaner[:mongoid].clean   
      end
    
      config.use_transactional_fixtures = false  
      config.infer_spec_type_from_file_location! 
    end
    
  3. Create your .rspec file (or edit yours if you already have it) to require spec_helper and rails_helper without having to call it manually in every single spec. I'd also suggest for everyone to color and format the spec output.

    # .rspec
    --color
    --format documentation
    --require spec_helper
    --require rails_helper