FactoryGirl无法访问我的模型

时间:2017-01-12 15:43:08

标签: ruby-on-rails rspec ruby-on-rails-5

问题

当我尝试运行bundle exec rspec时,我总是收到此错误:

Failure/Error: before { @user = FactoryGirl.build(:user) }

     NameError:
       uninitialized constant User

为什么FactoryGirl不能看到我的型号?以下是我的一些文件,供参考:

文件

我的工厂:

# spec/factories/users.rb
FactoryGirl.define do
  factory :user, class: User do
    email { FFaker::Internet.email }
    password "12345678"
    password_confirmation "12345678"
  end
end

我的模特:

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  has_many :jogs
end

我的规格:

require 'spec_helper'

describe 'User' do
  before { @user = FactoryGirl.build(:user) }

  subject { @user }

  it { is_expected.to respond_to(:email) }
  it { is_expected.to respond_to(:password) }
  it { is_expected.to respond_to(:password_confirmation) }

  it { is_expected.to be_valid }
end

我的spec_helper(没有所有评论):

require 'factory_girl_rails'
FactoryGirl.find_definitions

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

  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

  config.shared_context_metadata_behavior = :apply_to_host_groups

我的rails_helper(没有评论):

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|


  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  config.use_transactional_fixtures = true

  config.infer_spec_type_from_file_location!

  config.filter_rails_from_backtrace!
end

1 个答案:

答案 0 :(得分:4)

您的规范中应该有require 'rails_helper'而不是require 'spec_helper'