【问题标题】:rspec and gem bullet test env failrspec 和 gem 子弹测试环境失败
【发布时间】:2016-05-19 12:44:56
【问题描述】:

我想运行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)

请帮我解决这个问题

【问题讨论】:

  • 您是否尝试过在规范助手中明确添加require 'bullet'
  • 或者,你试过运行bundle exec rspec吗?
  • addind require 没有帮助。关于bundle exec - 我没有把它包括在我的问题中,因为我认为这是可以推测的。感谢您注意到这一点
  • 你能试试'bundle list'看看gem是否真的作为你的bundle的一部分安装了吗?
  • % bundle list | grep bullet * bullet (5.0.0)

标签: ruby-on-rails ruby rspec rubygems


【解决方案1】:

在我这边,我需要添加

require "active_record"
require "bullet"

spec/spec_helper.rbenvironments/test.rb 中都有

【讨论】:

    【解决方案2】:

    您需要将以下字符串移动到 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
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-03
        • 2011-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多