Module: RuboCop::RSpec::ExpectOffense
- Defined in:
- lib/rubocop/rspec/expect_offense.rb
Overview
Mixin for ‘expect_offense` and `expect_no_offenses`
This mixin makes it easier to specify strict offense expectations and a declarative and visual fashion. Just type out the code that should generate a offense, annotate code by writing ‘^’s underneath each character that should be highlighted, and follow the carets with a string (separated by a space) that is the message of the offense. You can include multiple offenses in one code snippet.
If you do not want to specify an offense then use the companion method ‘expect_no_offenses`. This method is a much simpler assertion since it just inspects the source and checks that there were no offenses. The `expect_offenses` method has to do more work by parsing out lines that contain carets.
Defined Under Namespace
Classes: AnnotatedSource
Constant Summary collapse
- DEFAULT_FILENAME =
'example.rb'.freeze
Instance Method Summary collapse
- #expect_no_offenses(source, filename = DEFAULT_FILENAME) ⇒ Object
- #expect_offense(source, filename = DEFAULT_FILENAME) ⇒ Object
Instance Method Details
#expect_no_offenses(source, filename = DEFAULT_FILENAME) ⇒ Object
62 63 64 65 66 |
# File 'lib/rubocop/rspec/expect_offense.rb', line 62 def expect_no_offenses(source, filename = DEFAULT_FILENAME) inspect_source(cop, source, filename) expect(cop.offenses).to be_empty end |
#expect_offense(source, filename = DEFAULT_FILENAME) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rubocop/rspec/expect_offense.rb', line 49 def expect_offense(source, filename = DEFAULT_FILENAME) expected_annotations = AnnotatedSource.parse(source) if expected_annotations.plain_source == source raise 'Use expect_no_offenses to assert that no offenses are found' end inspect_source(cop, expected_annotations.plain_source, filename) actual_annotations = expected_annotations.with_offense_annotations(cop.offenses) expect(expected_annotations.to_s).to eq(actual_annotations.to_s) end |