Created
September 2, 2021 22:05
-
-
Save timuruski/c2c50b7cfaafbb0fdcd1459b28c1dfd4 to your computer and use it in GitHub Desktop.
Simple, composable Bugsnag matcher.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RSpec::Matchers.define :notify_bugsnag do |error = nil, message = nil| | |
supports_block_expectations | |
match do |block| | |
if error | |
expect(Bugsnag).to receive(:notify).with(instance_of(error)) | |
else | |
expect(Bugsnag).to receive(:notify) | |
end | |
yield_to block | |
end | |
match_when_negated do |block| | |
expect(Bugsnag).to_not receive(:notify) | |
yield_to block | |
end | |
# Used to chain an assertion about the return value of the block. | |
# eg. expect { boop }.to notify_bugsnag.and eq "abc123" | |
chain :and do |matcher| | |
@result_matcher = matcher | |
end | |
private def yield_to(block) | |
# Suppresses a warning when not chained with "and" | |
if instance_variable_defined?(:@result_matcher) && @result_matcher | |
expect(block.yield).to @result_matcher | |
else | |
block.yield | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment