|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe RuboCop::Cop::RSpec::OrderedHooks do |
| 4 | + it 'detects `before` after `around`' do |
| 5 | + expect_offense(<<~RUBY) |
| 6 | + describe "hooks order" do |
| 7 | + around do |example| |
| 8 | + example.call |
| 9 | + end |
| 10 | +
|
| 11 | + before do |
| 12 | + ^^^^^^ `before` is supposed to appear before `around` at line 2. |
| 13 | + run_setup |
| 14 | + end |
| 15 | + end |
| 16 | + RUBY |
| 17 | + end |
| 18 | + |
| 19 | + it 'detects `before` after `after`' do |
| 20 | + expect_offense(<<~RUBY) |
| 21 | + describe "hooks order" do |
| 22 | + after do |
| 23 | + run_teardown |
| 24 | + end |
| 25 | +
|
| 26 | + before do |
| 27 | + ^^^^^^ `before` is supposed to appear before `after` at line 2. |
| 28 | + run_setup |
| 29 | + end |
| 30 | + end |
| 31 | + RUBY |
| 32 | + end |
| 33 | + |
| 34 | + it 'detects `around` after `after`' do |
| 35 | + expect_offense(<<~RUBY) |
| 36 | + describe "hooks order" do |
| 37 | + after do |
| 38 | + run_teardown |
| 39 | + end |
| 40 | +
|
| 41 | + around do |example| |
| 42 | + ^^^^^^ `around` is supposed to appear before `after` at line 2. |
| 43 | + example.run |
| 44 | + end |
| 45 | + end |
| 46 | + RUBY |
| 47 | + end |
| 48 | + |
| 49 | + context 'with multiple hooks' do |
| 50 | + it 'reports each violation independently' do |
| 51 | + expect_offense(<<~RUBY) |
| 52 | + describe "hooks order" do |
| 53 | + after { cleanup } |
| 54 | +
|
| 55 | + around do |example| |
| 56 | + ^^^^^^ `around` is supposed to appear before `after` at line 2. |
| 57 | + example.call |
| 58 | + end |
| 59 | +
|
| 60 | + before { some_preparation } |
| 61 | + ^^^^^^ `before` is supposed to appear before `around` at line 4. |
| 62 | +
|
| 63 | + before { some_other_preparation } |
| 64 | +
|
| 65 | + after { more_cleanup } |
| 66 | +
|
| 67 | + before { more_preparation } |
| 68 | + ^^^^^^ `before` is supposed to appear before `after` at line 12. |
| 69 | + end |
| 70 | + RUBY |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + context 'with scoped hooks' do |
| 75 | + context 'when hook is before' do |
| 76 | + it 'detects `suite` after `context`' do |
| 77 | + expect_offense(<<~RUBY) |
| 78 | + describe "hooks order" do |
| 79 | + before(:context) { init_factories } |
| 80 | + before(:suite) { global_setup } |
| 81 | + ^^^^^^^^^^^^^^ `before(:suite)` is supposed to appear before `before(:context)` at line 2. |
| 82 | + end |
| 83 | + RUBY |
| 84 | + end |
| 85 | + |
| 86 | + it 'detects `suite` after `each`' do |
| 87 | + expect_offense(<<~RUBY) |
| 88 | + describe "hooks order" do |
| 89 | + before(:each) { init_data } |
| 90 | + before(:suite) { global_setup } |
| 91 | + ^^^^^^^^^^^^^^ `before(:suite)` is supposed to appear before `before(:each)` at line 2. |
| 92 | + end |
| 93 | + RUBY |
| 94 | + end |
| 95 | + |
| 96 | + it 'detects `context` after `each`' do |
| 97 | + expect_offense(<<~RUBY) |
| 98 | + describe "hooks order" do |
| 99 | + before(:each) { init_data } |
| 100 | + before(:context) { setup } |
| 101 | + ^^^^^^^^^^^^^^^^ `before(:context)` is supposed to appear before `before(:each)` at line 2. |
| 102 | + end |
| 103 | + RUBY |
| 104 | + end |
| 105 | + |
| 106 | + it 'accepts `example` and `each`' do |
| 107 | + expect_no_offenses(<<~RUBY) |
| 108 | + describe "hooks order" do |
| 109 | + before { setup1 } |
| 110 | + before(:each) { setup2 } |
| 111 | + before(:example) { setup3 } |
| 112 | + end |
| 113 | + RUBY |
| 114 | + end |
| 115 | + |
| 116 | + it 'detects `context` after `example`' do |
| 117 | + expect_offense(<<~RUBY) |
| 118 | + describe "hooks order" do |
| 119 | + before(:example) { init_data } |
| 120 | + before(:context) { setup } |
| 121 | + ^^^^^^^^^^^^^^^^ `before(:context)` is supposed to appear before `before(:example)` at line 2. |
| 122 | + end |
| 123 | + RUBY |
| 124 | + end |
| 125 | + end |
| 126 | + |
| 127 | + context 'when hook is after' do |
| 128 | + it 'detects `context` after `suite`' do |
| 129 | + expect_offense(<<~RUBY) |
| 130 | + describe "hooks order" do |
| 131 | + after(:suite) { global_teardown } |
| 132 | + after(:context) { teardown } |
| 133 | + ^^^^^^^^^^^^^^^ `after(:context)` is supposed to appear before `after(:suite)` at line 2. |
| 134 | + end |
| 135 | + RUBY |
| 136 | + end |
| 137 | + |
| 138 | + it 'detects `each` after `suite`' do |
| 139 | + expect_offense(<<~RUBY) |
| 140 | + describe "hooks order" do |
| 141 | + after(:suite) { global_teardown } |
| 142 | + after(:each) { teardown } |
| 143 | + ^^^^^^^^^^^^ `after(:each)` is supposed to appear before `after(:suite)` at line 2. |
| 144 | + end |
| 145 | + RUBY |
| 146 | + end |
| 147 | + |
| 148 | + it 'detects `each` after `context`' do |
| 149 | + expect_offense(<<~RUBY) |
| 150 | + describe "hooks order" do |
| 151 | + after(:context) { teardown } |
| 152 | + after(:each) { cleanup } |
| 153 | + ^^^^^^^^^^^^ `after(:each)` is supposed to appear before `after(:context)` at line 2. |
| 154 | + end |
| 155 | + RUBY |
| 156 | + end |
| 157 | + |
| 158 | + it 'accepts `example` and `each`' do |
| 159 | + expect_no_offenses(<<~RUBY) |
| 160 | + describe "hooks order" do |
| 161 | + after { setup1 } |
| 162 | + after(:each) { setup2 } |
| 163 | + after(:example) { setup3 } |
| 164 | + end |
| 165 | + RUBY |
| 166 | + end |
| 167 | + |
| 168 | + it 'detects `example` after `context`' do |
| 169 | + expect_offense(<<~RUBY) |
| 170 | + describe "hooks order" do |
| 171 | + after(:context) { cleanup } |
| 172 | + after(:example) { teardown } |
| 173 | + ^^^^^^^^^^^^^^^ `after(:example)` is supposed to appear before `after(:context)` at line 2. |
| 174 | + end |
| 175 | + RUBY |
| 176 | + end |
| 177 | + end |
| 178 | + end |
| 179 | + |
| 180 | + it 'accepts hooks in order' do |
| 181 | + expect_no_offenses(<<~RUBY) |
| 182 | + desribe "correctly ordered hooks" do |
| 183 | + before(:suite) do |
| 184 | + run_global_setup |
| 185 | + end |
| 186 | +
|
| 187 | + before(:context) do |
| 188 | + run_context_setup |
| 189 | + end |
| 190 | +
|
| 191 | + before(:example) do |
| 192 | + run_setup |
| 193 | + end |
| 194 | +
|
| 195 | + around(:example) do |example| |
| 196 | + example.run |
| 197 | + end |
| 198 | +
|
| 199 | + after(:example) do |
| 200 | + run_teardown |
| 201 | + end |
| 202 | +
|
| 203 | + after(:context) do |
| 204 | + run_context_teardown |
| 205 | + end |
| 206 | +
|
| 207 | + after(:suite) do |
| 208 | + run_global_teardown |
| 209 | + end |
| 210 | + end |
| 211 | + RUBY |
| 212 | + end |
| 213 | +end |
0 commit comments