Skip to content

Instantly share code, notes, and snippets.

@timuruski
Created September 2, 2021 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timuruski/c2c50b7cfaafbb0fdcd1459b28c1dfd4 to your computer and use it in GitHub Desktop.
Save timuruski/c2c50b7cfaafbb0fdcd1459b28c1dfd4 to your computer and use it in GitHub Desktop.
Simple, composable Bugsnag matcher.
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