您如何测试自定义 Bugsnag meta_data(在 Ruby 中,使用 Rspec)?
我要测试的代码:
def do_something
thing_that_could_error
rescue => e
Bugsnag.notify(e) do |r|
r.meta_data = { my_extra_data: "useful info" }
end
end
我想写的测试:
context "when there's an error" do
it "calls Bugsnag with my special metadata" do
expect(Bugsnag).to receive(:notify) # TODO test meta_data values contain "my useful info"
expect do
do_something() # exception is thrown and rescued and sent to Bugsnag
end.not_to raise_error
end
end
我在用:
- 红宝石 2.6.6
- 规范 3.9.0
- Bugsnag 6.17.0 https://rubygems.org/gems/bugsnag
变量内部的数据meta_data
比这个小例子复杂得多,这就是我想测试它的原因。在一个美好的世界里,我会把这个逻辑提取到一个助手中并测试这个助手,但现在现场测试是紧迫和有用的。
我一直在研究 Bugsnag gem 的内部来解决这个问题(加上一些 Rspec-fu 来捕获各种内部状态和返回的数据),但在某些时候,询问互联网是个好主意。