首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我指定环境时,为什么RSpec不能在涉及Sidekiq的测试中找到Airbrake键?

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

Stack Overflow用户
提问于 2016-01-06 00:16:07
回答 1查看 577关注 0票数 3

这是我的设置:

airbrake.rb

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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测试,则会得到以下错误:

代码语言:javascript
复制
Airbrake::Error:
       the 'default' notifier isn't configured

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

代码语言:javascript
复制
Airbrake.configure do |c|
  c.project_id = ENV['PROJECT_ID']
  c.project_key = ENV['PROJECT_KEY']
end

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

EN

回答 1

Stack Overflow用户

发布于 2016-02-22 16:24:51

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34623599

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档