当我指定环境时,为什么RSpec在涉及Sidekiq的测试中找不到Airbrake env键?

时间:2016-01-06 00:16:08

标签: ruby rspec sidekiq airbrake

这是我的设置:

airbrake.rb

require 'airbrake'

Airbrake.configure do |c|
  c.ignore_environments = [:test, :development]
  c.project_id = ENV['PROJECT_ID']
  c.project_key = ENV['PROJECT_KEY']
end

use Airbrake::Rack::Middleware

spec_helper.rb

RSpec.configure do |config|
  config.before(:suite) do
    FactoryGirl.reload
    FactoryGirl.define do
      to_create { |instance| instance.save }
    end
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
    Airbrake.configure(:test) do |c|
      c.project_id = ENV['PROJECT_ID']
      c.project_key = ENV['PROJECT_KEY']
    end
  end

  config.around(:each) do |example|
    DatabaseCleaner.cleaning do
      example.run
    end
  end

  config.include FactoryGirl::Syntax::Methods
end

worker_test_spec.rb

require 'spec_helper'

RSpec.describe NotificationWorker do
  it "perform should call Airbrake#notify" do
    anotification_worker = LNotificationWorker.new
    airbrake_notification_worker.perform("some error message"))
    expect(Airbrake).to receive(:notify).with("some error message")
  end
end

我在其他(非Sidekiq)测试中调用Airbrake#notify,他们找到合适的ENV变量就好了。

然而,如果我使用上述设置运行上述Sidekiq测试,我会收到以下错误:

Airbrake::Error:
       the 'default' notifier isn't configured

但是如果我将spec_helper.rb中的Airbrake配置更改为:

Airbrake.configure do |c|
  c.project_id = ENV['PROJECT_ID']
  c.project_key = ENV['PROJECT_KEY']
end

ENV键可以在测试中找到。这是为什么?

1 个答案:

答案 0 :(得分:1)

当您说Airbrake.configure(:test)时,并不意味着"为test RAILS_ENV"配置Airbrake。相反,:test会创建一个非默认的named notifier。然后,您可以通过说Airbrake.notify("oops", {time: Time.now}, :test)向该通知程序发送特定通知。但这不是关于开发/测试/制作,而是关于对通知进行分类。

所以问题是你已经配置了一个名为test的通知程序,但你还没有配置一个名为default的通知程序,而default就是Airbrake在你做的时候想要使用的# 39;否则告诉它。当您简单地说Airbrake.configure { ... }时,这就是您的规范通过的原因。