|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe RuboCop::Cop::Committee::MultipleSchemaConform, :config do |
| 4 | + it "registers an offense for multiple assertions in the same request block" do |
| 5 | + expect_offense(<<~RUBY) |
| 6 | + it 'returns users' do |
| 7 | + get '/users' |
| 8 | + assert_schema_conform(200) |
| 9 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid multiple schema conformance assertions in the same request block. |
| 10 | + assert_response_schema_confirm(200) |
| 11 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid multiple schema conformance assertions in the same request block. |
| 12 | + end |
| 13 | + RUBY |
| 14 | + end |
| 15 | + |
| 16 | + it "registers offenses for duplicate assertions in the same request block" do |
| 17 | + expect_offense(<<~RUBY) |
| 18 | + it 'returns users' do |
| 19 | + get '/users' |
| 20 | + assert_schema_conform(200) |
| 21 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid multiple schema conformance assertions in the same request block. |
| 22 | + assert_schema_conform(200) |
| 23 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid multiple schema conformance assertions in the same request block. |
| 24 | + end |
| 25 | + RUBY |
| 26 | + end |
| 27 | + |
| 28 | + it "does not register an offense for a single assertion" do |
| 29 | + expect_no_offenses(<<~RUBY) |
| 30 | + it 'returns users' do |
| 31 | + get '/users' |
| 32 | + assert_schema_conform(200) |
| 33 | + end |
| 34 | + RUBY |
| 35 | + end |
| 36 | + |
| 37 | + it "does not register an offense when assertions are in separate request blocks" do |
| 38 | + expect_no_offenses(<<~RUBY) |
| 39 | + it 'creates and retrieves user' do |
| 40 | + post '/users', params: { name: 'test' } |
| 41 | + assert_schema_conform(201) |
| 42 | +
|
| 43 | + get '/users/1' |
| 44 | + assert_schema_conform(200) |
| 45 | + end |
| 46 | + RUBY |
| 47 | + end |
| 48 | + |
| 49 | + it "registers an offense for request and response assertions in the same request block" do |
| 50 | + expect_offense(<<~RUBY) |
| 51 | + it 'returns users' do |
| 52 | + get '/users' |
| 53 | + assert_request_schema_confirm |
| 54 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid multiple schema conformance assertions in the same request block. |
| 55 | + assert_response_schema_confirm(200) |
| 56 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid multiple schema conformance assertions in the same request block. |
| 57 | + end |
| 58 | + RUBY |
| 59 | + end |
| 60 | + |
| 61 | + it "registers offenses for all assertion combinations in the same request block" do |
| 62 | + expect_offense(<<~RUBY) |
| 63 | + it 'returns users' do |
| 64 | + get '/users' |
| 65 | + assert_request_schema_confirm |
| 66 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid multiple schema conformance assertions in the same request block. |
| 67 | + assert_response_schema_confirm(200) |
| 68 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid multiple schema conformance assertions in the same request block. |
| 69 | + assert_schema_conform(200) |
| 70 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid multiple schema conformance assertions in the same request block. |
| 71 | + end |
| 72 | + RUBY |
| 73 | + end |
| 74 | + |
| 75 | + it "registers an offense for request assertion with schema conform in the same request block" do |
| 76 | + expect_offense(<<~RUBY) |
| 77 | + it 'returns users' do |
| 78 | + get '/users' |
| 79 | + assert_request_schema_confirm |
| 80 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid multiple schema conformance assertions in the same request block. |
| 81 | + assert_schema_conform(200) |
| 82 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid multiple schema conformance assertions in the same request block. |
| 83 | + end |
| 84 | + RUBY |
| 85 | + end |
| 86 | + |
| 87 | + it "registers an offense for response assertion with schema conform in the same request block" do |
| 88 | + expect_offense(<<~RUBY) |
| 89 | + it 'returns users' do |
| 90 | + get '/users' |
| 91 | + assert_response_schema_confirm(200) |
| 92 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid multiple schema conformance assertions in the same request block. |
| 93 | + assert_schema_conform(200) |
| 94 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid multiple schema conformance assertions in the same request block. |
| 95 | + end |
| 96 | + RUBY |
| 97 | + end |
| 98 | +end |
0 commit comments