【问题标题】:Uninitialized constant Name Error when run rspec, but works fine in rails console运行rspec时未初始化的常量名称错误,但在rails控制台中工作正常
【发布时间】:2016-04-28 11:29:10
【问题描述】:

我在使用 FactoryGirl 运行 rspecs 时收到“nameError: uninitialized constant”错误,但是当我通过运行 rails c test 在控制台内调用 FactoryGirl.build 时,它可以正确识别 FactoryGirl。

这是 rspec 的输出:

Failures:

1) create user test
 Failure/Error: FactoryGirl.build(:user)

 NameError:
   uninitialized constant User
 # /Users/rapha/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:261:in `const_get'
 # /Users/rapha/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:261:in `block in constantize'
 # /Users/rapha/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:259:in `each'
 # /Users/rapha/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:259:in `inject'
 # /Users/rapha/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:259:in `constantize'
 # /Users/rapha/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/core_ext/string/inflections.rb:66:in `constantize'
 # /Users/rapha/.rvm/gems/ruby-2.2.3/gems/factory_girl-4.5.0/lib/factory_girl/factory.rb:26:in `build_class'
 # /Users/rapha/.rvm/gems/ruby-2.2.3/gems/factory_girl-4.5.0/lib/factory_girl/factory.rb:37:in `run'
 # /Users/rapha/.rvm/gems/ruby-2.2.3/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:23:in `block in run'
 # /Users/rapha/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5/lib/active_support/notifications.rb:166:in `instrument'
 # /Users/rapha/.rvm/gems/ruby-2.2.3/gems/factory_girl-4.5.0/lib/factory_girl/factory_runner.rb:22:in `run'
 # /Users/rapha/.rvm/gems/ruby-2.2.3/gems/factory_girl-4.5.0/lib/factory_girl/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
 # ./spec/model/user_spec.rb:7:in `block (2 levels) in <top (required)>'

 Finished in 0.001 seconds (files took 0.31845 seconds to load)

1 个示例,1 个失败

/spec/spec_helper.rb:

require 'factory_girl_rails'

RSpec.configure do |config|

  config.include FactoryGirl::Syntax::Methods
  config.before do
    FactoryGirl.find_definitions
  end
  ...
end

/spec/rails_helper.rb:

require 'spec_helper'
require 'rspec/rails'
require 'capybara/rspec'
require 'factory_girl_rails'

RSpec.configure do |config|

  config.include FactoryGirl::Syntax::Methods

  ...

end

/spec/model/user_spec.rb:

 require 'spec_helper'

 # Create factory de user
 describe "create user" do
  it "test" do
    u = FactoryGirl.build(:user)
  end
end

/Gemfile:

group :development, :test do
  gem 'rspec-rails'
  gem 'guard-rspec'
  gem 'capybara'
  gem 'factory_girl_rails'

end

【问题讨论】:

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


    【解决方案1】:

    解决NameError

    • 您需要将 FactoryGirl 的配置从 /spec/spec_helper.rb 移动到 /spec/rails_helper.rb

    • 您需要将/spec/model/user_spec.rb 中的spec_helper 替换为rails helper

    请尝试一下。

    【讨论】:

      【解决方案2】:

      您是否有机会使用 rspec 3? 从 rspec 3 开始,您需要 require 'rails_helper' 而不是 spec_helper

      https://github.com/rspec/rspec-rails#upgrade-note

      【讨论】:

      • 是的,我使用的是 rspec 3.4。我刚刚测试了您所说的内容,并且在进行了一些小修复后一切正常。我需要从 rails_helper.rb 中删除“require 'factory_girl_rails'”和“config.include FactoryGirl::Syntax::Methods”行,并且还必须从 spec_helper.rb 中删除“config.before do FactoryGirl.find_definitions end” /跨度>
      • 在 /spec/model/user_spec.rb 中,尝试使用require 'rails_helper'
      猜你喜欢
      • 1970-01-01
      • 2015-05-03
      • 1970-01-01
      • 1970-01-01
      • 2016-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多