rspec 和 gem 子弹测试环境失败

rspec and gem bullet test env fail

我想 运行 rspecgem bullet 找出所有弱点。 我正在做如下:

宝石文件

group :development, :test do
  gem "bullet"
end

config/environments/test.rb

  config.after_initialize do
    Bullet.enable = true
    Bullet.rails_logger = true
    Bullet.raise = true # raise an error if n+1 query occurs
  end

spec/spec_helper.rb

    if Bullet.enable?
    config.before(:each) do
      Bullet.start_request
    end

    config.after(:each) do
      Bullet.perform_out_of_channel_notifications if Bullet.notification?
      Bullet.end_request
    end
  end

但是当我尝试 运行 rspec 时出现错误

% bundle exec rspec
/spec/spec_helper.rb:24:in `block in <top (required)>': uninitialized constant Bullet (NameError)

请帮我解决这个问题

您需要将以下字符串移动到 spec/rails_helper.rb 文件

RSpec.configure do |config|

  ...

  if Bullet.enable?
    config.before(:each) do
      Bullet.start_request
    end

    config.after(:each) do
      Bullet.perform_out_of_channel_notifications if Bullet.notification?
      Bullet.end_request
    end
  end

  ...

end

我这边需要补充

require "active_record"
require "bullet"

spec/spec_helper.rbenvironments/test.rb

我遇到了同样的问题,我发现这是因为在我的 Gemfile 中,项目符号在组 :development 中,但不在组 :test 中。