|
| 1 | +RSpec.describe RuboCop::Cop::RSpec::UnspecifiedException do |
| 2 | + subject(:cop) { described_class.new } |
| 3 | + |
| 4 | + context 'with raise_error matcher' do |
| 5 | + it 'detects the `unspecified_exception` offense' do |
| 6 | + expect_offense(<<-RUBY) |
| 7 | + expect { |
| 8 | + raise StandardError |
| 9 | + }.to raise_error |
| 10 | + ^^^^^^^^^^^ Specify the exception being captured |
| 11 | + RUBY |
| 12 | + end |
| 13 | + |
| 14 | + it 'allows empty exception specification when not expecting an error' do |
| 15 | + expect_no_offenses(<<-RUBY) |
| 16 | + expect { |
| 17 | + raise StandardError |
| 18 | + }.not_to raise_error |
| 19 | + RUBY |
| 20 | + end |
| 21 | + |
| 22 | + it 'allows exception classes' do |
| 23 | + expect_no_offenses(<<-RUBY) |
| 24 | + expect { |
| 25 | + raise StandardError |
| 26 | + }.to raise_error(StandardError) |
| 27 | + RUBY |
| 28 | + end |
| 29 | + |
| 30 | + it 'allows exception messages' do |
| 31 | + expect_no_offenses(<<-RUBY) |
| 32 | + expect { |
| 33 | + raise StandardError.new('error') |
| 34 | + }.to raise_error('error') |
| 35 | + RUBY |
| 36 | + end |
| 37 | + |
| 38 | + it 'allows exception types with messages' do |
| 39 | + expect_no_offenses(<<-RUBY) |
| 40 | + expect { |
| 41 | + raise StandardError.new('error') |
| 42 | + }.to raise_error(StandardError, 'error') |
| 43 | + RUBY |
| 44 | + end |
| 45 | + |
| 46 | + it 'allows exception matching regular expressions' do |
| 47 | + expect_no_offenses(<<-RUBY) |
| 48 | + expect { |
| 49 | + raise StandardError.new('error') |
| 50 | + }.to raise_error(/err/) |
| 51 | + RUBY |
| 52 | + end |
| 53 | + |
| 54 | + it 'allows exception types with matching regular expressions' do |
| 55 | + expect_no_offenses(<<-RUBY) |
| 56 | + expect { |
| 57 | + raise StandardError.new('error') |
| 58 | + }.to raise_error(StandardError, /err/) |
| 59 | + RUBY |
| 60 | + end |
| 61 | + |
| 62 | + it 'allows classes with blocks with braces' do |
| 63 | + expect_no_offenses(<<-RUBY) |
| 64 | + expect { |
| 65 | + raise StandardError.new('error') |
| 66 | + }.to raise_error { |err| err.data } |
| 67 | + RUBY |
| 68 | + end |
| 69 | + |
| 70 | + it 'allows classes with blocks with do/end' do |
| 71 | + expect_no_offenses(<<-RUBY) |
| 72 | + expect { |
| 73 | + raise StandardError.new('error') |
| 74 | + }.to raise_error do |error| |
| 75 | + error.data |
| 76 | + end |
| 77 | + RUBY |
| 78 | + end |
| 79 | + |
| 80 | + it 'allows parameterized exceptions' do |
| 81 | + expect_no_offenses(<<-RUBY) |
| 82 | + my_exception = StandardError.new('my exception') |
| 83 | + expect { |
| 84 | + raise my_exception |
| 85 | + }.to raise_error(my_exception) |
| 86 | + RUBY |
| 87 | + end |
| 88 | + end |
| 89 | + |
| 90 | + context 'with raise_exception matcher' do |
| 91 | + it 'detects the `unspecified_exception` offense' do |
| 92 | + expect_offense(<<-RUBY) |
| 93 | + expect { |
| 94 | + raise StandardError |
| 95 | + }.to raise_exception |
| 96 | + ^^^^^^^^^^^^^^^ Specify the exception being captured |
| 97 | + RUBY |
| 98 | + end |
| 99 | + |
| 100 | + it 'allows empty exception specification when not expecting an error' do |
| 101 | + expect_no_offenses(<<-RUBY) |
| 102 | + expect { |
| 103 | + raise StandardError |
| 104 | + }.not_to raise_exception |
| 105 | + RUBY |
| 106 | + end |
| 107 | + |
| 108 | + it 'allows exception classes' do |
| 109 | + expect_no_offenses(<<-RUBY) |
| 110 | + expect { |
| 111 | + raise StandardError |
| 112 | + }.to raise_exception(StandardError) |
| 113 | + RUBY |
| 114 | + end |
| 115 | + |
| 116 | + it 'allows exception messages' do |
| 117 | + expect_no_offenses(<<-RUBY) |
| 118 | + expect { |
| 119 | + raise StandardError.new('error') |
| 120 | + }.to raise_exception('error') |
| 121 | + RUBY |
| 122 | + end |
| 123 | + |
| 124 | + it 'allows exception types with messages' do |
| 125 | + expect_no_offenses(<<-RUBY) |
| 126 | + expect { |
| 127 | + raise StandardError.new('error') |
| 128 | + }.to raise_exception(StandardError, 'error') |
| 129 | + RUBY |
| 130 | + end |
| 131 | + |
| 132 | + it 'allows exception matching regular expressions' do |
| 133 | + expect_no_offenses(<<-RUBY) |
| 134 | + expect { |
| 135 | + raise StandardError.new('error') |
| 136 | + }.to raise_exception(/err/) |
| 137 | + RUBY |
| 138 | + end |
| 139 | + |
| 140 | + it 'allows exception types with matching regular expressions' do |
| 141 | + expect_no_offenses(<<-RUBY) |
| 142 | + expect { |
| 143 | + raise StandardError.new('error') |
| 144 | + }.to raise_exception(StandardError, /err/) |
| 145 | + RUBY |
| 146 | + end |
| 147 | + |
| 148 | + it 'allows classes with blocks with braces' do |
| 149 | + expect_no_offenses(<<-RUBY) |
| 150 | + expect { |
| 151 | + raise StandardError.new('error') |
| 152 | + }.to raise_exception { |err| err.data } |
| 153 | + RUBY |
| 154 | + end |
| 155 | + |
| 156 | + it 'allows classes with blocks with do/end' do |
| 157 | + expect_no_offenses(<<-RUBY) |
| 158 | + expect { |
| 159 | + raise StandardError.new('error') |
| 160 | + }.to raise_exception do |error| |
| 161 | + error.data |
| 162 | + end |
| 163 | + RUBY |
| 164 | + end |
| 165 | + |
| 166 | + it 'allows parameterized exceptions' do |
| 167 | + expect_no_offenses(<<-RUBY) |
| 168 | + my_exception = StandardError.new('my exception') |
| 169 | + expect { |
| 170 | + raise my_exception |
| 171 | + }.to raise_exception(my_exception) |
| 172 | + RUBY |
| 173 | + end |
| 174 | + end |
| 175 | +end |
0 commit comments