3

这是我的设置:

空气制动器.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 密钥可以在测试中找到。为什么是这样?

4

1 回答 1

1

当你说的时候Airbrake.configure(:test),它并不意味着“配置 Airbrake for the test RAILS_ENV”。而是:test创建一个非默认命名的通知程序。然后,您可以通过说 向该通知者发送特定通知Airbrake.notify("oops", {time: Time.now}, :test)。但这不是关于开发/测试/生产,而是关于对通知进行分类。

所以问题是你已经配置了一个名为的通知器test,但你还没有配置一个名为的通知器,当你不告诉它时,它就是 Airbrake 想要使用的defaultdefault这就是为什么当您简单地说时您的规范通过了Airbrake.configure { ... }

于 2016-02-22T16:24:51.923 回答