【问题标题】:RSpec error "Uninitialized constant FactoryGirl (Name Error)"RSpec 错误“未初始化的常量 FactoryGirl(名称错误)”
【发布时间】:2014-03-28 14:23:13
【问题描述】:

我尝试运行我的 RSpec 测试:

rspec comments.rb

但不断收到相同的错误(见标题)。在有人问之前,我已经将 require factory_girl 添加到 spec_helper.rb。

spec/factories/comments.rb文件的内容是:

  FactoryGirl.define do
    factory :comment do
      comment "MyString"
      task_id 1
  end
end

这是我包含 factory_girl_rails 的 gemfile 的测试组:

source 'http://rubygems.org'

...

group :development, :test do
  gem 'debugger'
  gem 'factory_girl_rails'
  gem 'rspec-rails'
end

...

【问题讨论】:

    标签: ruby-on-rails rspec


    【解决方案1】:

    您只需在您的 spec_helper.rb 文件中添加以下内容

    require 'factory_girl_rails'
    

    【讨论】:

    • FactoryGirl 已更名为 FactoryBot,因此您现在需要使用 require 'factory_bot_rails'
    【解决方案2】:

    这样做:

    FactoryGirl.create(:artist).should be_valid 
    

    【讨论】:

    • 这个答案使用了未知常量FactoryGirl。这应该有什么帮助?
    • 我投票删除这个答案,因为它没有解决所提出的问题。
    猜你喜欢
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 2016-07-15
    • 1970-01-01
    相关资源
    最近更新 更多