Factory_Girl Rspec的“未初始化的常量用户”

时间:2015-04-23 13:04:33

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

我正在尝试为我的用户模型设置Factory Factory工厂,如果没有出现某种错误,我似乎无法得到它。我现在得到的错误是“未初始化的常量用户”

规格/ factories.rb

factory :user do |user|

user.name "John Doe"
user.admin false
user.created_at Time.now
user.updated_at Time.now
user.password_digest nil
user.remember_token nil
user.e_admin nil
user.t_admin true
user.d_admin false
user.activated false
user.activated_at Time.now
end

规格/ spec_helper.rb

require 'rails/all'
require 'rspec/rails'
require 'factory_girl_rails'

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

  config.filter_run :focus
  config.run_all_when_everything_filtered = true

  config.disable_monkey_patching!
  config.warnings = true
  if config.files_to_run.one?
    config.default_formatter = 'doc'
  end

  config.profile_examples = 10

  config.order = :random
  Kernel.srand config.seed

  config.include FactoryGirl::Syntax::Methods

  FactoryGirl.definition_file_paths = [File.expand_path('../factories', __FILE__)]
  FactoryGirl.find_definitions

end

规格/ user_spec.rb

require 'rspec'
require 'spec_helper'
require 'factory_girl_rails'


RSpec.describe 'Log In' do

  it 'should log user in' do

    var = create(:user)

    expect(var.valid?).to eql(true)
  end
 end

有谁知道如何解决此错误?

  1) Log In should log user in
     Failure/Error: var = FactoryGirl.create(:user)
     NameError:
       uninitialized constant User
     # /Users/ssuhli200/.rvm/gems/jruby-1.7.18@cimport/gems/activesupport-3.2.21/lib/active_support/inflector/methods.rb:230:in `constantize'
     # /Users/ssuhli200/.rvm/gems/jruby-1.7.18@cimport/gems/activesupport-3.2.21/lib/active_support/inflector/methods.rb:229:in `each'
     # /Users/ssuhli200/.rvm/gems/jruby-1.7.18@cimport/gems/activesupport-3.2.21/lib/active_support/inflector/methods.rb:229:in `constantize'
     # /Users/ssuhli200/.rvm/gems/jruby-1.7.18@cimport/gems/activesupport-3.2.21/lib/active_support/core_ext/string/inflections.rb:54:in `constantize'
     # /Users/ssuhli200/.rvm/gems/jruby-1.7.18@cimport/gems/factory_girl-4.5.0/lib/factory_girl/factory.rb:26:in `build_class'
     # /Users/ssuhli200/.rvm/gems/jruby-1.7.18@cimport/gems/factory_girl-4.5.0/lib/factory_girl/factory.rb:37:in `run'
     # /Users/ssuhli200/.rvm/gems/jruby-1.7.18@cimport/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:23:in `run'
     # /Users/ssuhli200/.rvm/gems/jruby-1.7.18@cimport/gems/activesupport-3.2.21/lib/active_support/notifications.rb:125:in `instrument'
     # /Users/ssuhli200/.rvm/gems/jruby-1.7.18@cimport/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:22:in `run'
     # /Users/ssuhli200/.rvm/gems/jruby-1.7.18@cimport/gems/factory_girl-4.5.0/lib/factory_girl/strategy_syntax_method_registrar.rb:20:in `create'
     # ./spec/user_spec.rb:10:in `(root)'

2 个答案:

答案 0 :(得分:6)

我想通了,我不得不在我的.rspec文件中添加--require rails_helper。然后我不得不拿出我的" FactoryGirl.find_definitions"因为我收到错误,因为我有一个以上的用户工厂。谢谢大家的帮助!

答案 1 :(得分:0)

你的工厂应该是这样的

FactoryGirl.define do
  factory :user do
    name "John Doe"
    admin false
    created_at Time.now
    updated_at Time.now
    password_digest nil
    remember_token nil
    e_admin nil
    t_admin true
    d_admin false
    activated false
    activated_at Time.now
  end
end

在文件夹结构中 - spec/factories/user.rb