【发布时间】:2016-01-18 19:35:28
【问题描述】:
我有一个正在测试的服务类。服务类对外部资源进行 API 调用。如果资源返回为空,我想提出一个使用ExceptionNotification gem 发送电子邮件的异常。
这个 gem 作为中间件运行,通常在测试环境中不启用。但是,在我的服务类测试中,我想测试异常通知是否消失,因为通知我们 API 请求失败非常重要。
我对中间件的理解是,它们通常是在请求和应用程序的上下文中。但是,如果我在config/environments/test.rb 中启用ExceptionNotification gem 并运行我的单元测试,则会发送异常通知电子邮件。
所以,我的问题是如何在没有“应用程序”的情况下临时打开和关闭这个中间件来进行这个测试,所以我认为这在没有功能规范的情况下必须是可能的。
好的,这是我正在使用并开始的代码:
class MyService
ThingNotFound = Class.new(Exception)
def self.doit(params)
the_thing = ApiResource.get_the_thing(params)
raise ThingNotFound unless the_thing.present?
return the_thing
rescue ThingNotFound => e
ExceptionNotifier.notify_exception(e, data: {params: params})
end
end
在config/environments/test.rb,这里是ExceptionNotifier中间件代码:
config.before_initialize do
MyApp::Application.config.middleware.use ExceptionNotification::Rack
end
【问题讨论】:
标签: ruby-on-rails ruby rspec rack middleware