【问题标题】:"Uninitialized constant User" for Factory_Girl RspecFactory_Girl Rspec 的“未初始化常量用户”
【发布时间】:2015-04-23 13:04:33
【问题描述】:

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

spec/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/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

spec/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)'

【问题讨论】:

  • 您在 app/models 目录中确实有一个 User 类,对吧?
  • 我认为这行不通,但试试 var = FactoryGirl.create(:user)
  • 添加 RSpec 后是否运行rails generate rspec:install
  • 我不这么认为,我没有“rails_helper.rb”。我刚刚运行它,但我得到了同样的错误。我必须向 rails_helper.rb 添加任何内容吗?

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


【解决方案1】:

我想通了,我必须将 --require rails_helper 添加到我的 .rspec 文件中。然后我不得不取出我的“FactoryGirl.find_definitions”行,因为我收到一个错误,我有多个用户工厂。感谢大家的帮助!

【讨论】:

    【解决方案2】:

    你的工厂应该是这样的

    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

    【讨论】:

    • 您的模型中是否确实有一个 user.rb 文件并运行 rake db:migrate 来创建用户?不得不问:)
    • 是的,我愿意。我再次运行迁移,但仍然没有运气。
    • 可能包括相关错误的堆栈跟踪。可能会更深入地了解错误。
    猜你喜欢
    • 2018-03-10
    • 2019-11-11
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    相关资源
    最近更新 更多