ruby on rails - Factory not registered: user (ArgumentError) -


i using rspec-rails, factory_girl_rails , mogoid_rspec gems. after adding factory girl gem, keep getting error factory not registered: user (argumenterror) user factory. following related code snippets:

in gemfile:

group :development, :test   gem 'byebug'   gem 'rspec-rails'   gem 'mongoid-rspec', '~> 2.1.0' end  group :test   gem 'database_cleaner'   gem 'faker'   gem 'shoulda-matchers'   gem 'factory_girl_rails' end 

rails_helper.rb

# file copied spec/ when run 'rails generate rspec:install' env['rails_env'] ||= 'test' require file.expand_path('../../config/environment', __file__)  # prevent database truncation if environment production abort("the rails environment 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 |config|   config.before(:all)     factorygirl.reload   end    config.include mongoid::matchers, type: :model    config.before(:suite)     databasecleaner[:mongoid].strategy = :truncation   end    config.before(:each)     databasecleaner[:mongoid].start   end    config.after(:each)     databasecleaner[:mongoid].clean   end    config.use_transactional_fixtures = false    config.infer_spec_type_from_file_location! end 

factories/user.rb

factorygirl.define   factory :user     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 |config|   config.include factorygirl::syntax::methods end 

spec_helper.rb

rspec.configure |config|    config.expect_with :rspec |expectations|      expectations.include_chain_clauses_in_custom_matcher_descriptions = true   end    config.mock_with :rspec |mocks|     mocks.verify_partial_doubles = true   end end 

any appreciated. in advance! :)

as we've discussed, i'm pasting proposition of change in code (including practices).

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

    #gemfile group :development, :test   gem 'byebug'   gem 'rspec-rails'   gem 'factory_girl_rails'   gem 'faker'   gem 'mongoid-rspec', '~> 2.1.0' end  group :test   gem 'database_cleaner'   gem 'shoulda-matchers' end 
  2. require support files in 1 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 environment production  abort("the rails environment running in production mode!") if rails.env.production?  dir[rails.root.join('spec/support/**/*.rb')].each { |f| require f }  rspec.configure |config|      config.before(:all)     factorygirl.reload      end    config.include mongoid::matchers, type: :model    config.before(:suite)     databasecleaner[:mongoid].strategy = :truncation      end    config.before(:each)     databasecleaner[:mongoid].start      end    config.after(:each)     databasecleaner[:mongoid].clean      end    config.use_transactional_fixtures = false     config.infer_spec_type_from_file_location!  end 
  3. create .rspec file (or edit yours if have it) require spec_helper , rails_helper without having call manually in every single spec. i'd suggest color , format spec output.

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

Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -