工厂未注册:user(ArgumentError)

时间:2015-07-07 13:44:33

标签: ruby-on-rails rspec mongoid factory-bot

我正在使用rspec-rails,factory_girl_rails和mogoid_rspec gems。添加工厂女孩宝石后,我不断收到我的用户工厂的错误Factory not registered: user (ArgumentError)。以下是相关的代码段:

在我的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

工厂/ 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

规格/支持/ 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

非常感谢任何帮助。提前致谢! :)

1 个答案:

答案 0 :(得分:0)

正如我们所讨论的那样,我正在粘贴我对你的代码中可能改变的内容(包括良好实践)的建议。

  1. factory_girl_rails内定义group :test, :development gem。

    #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. 将您的支持文件放在一行而不是乘法 定义

    # 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. 创建您的.rspec文件(如果您已经拥有,则编辑自己的文件) require spec_helperrails_helper,无需在每个规范中手动调用它。 我也建议每个人都这样做 颜色并格式化规格输出。

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