|
3 | 3 | require 'minitest' |
4 | 4 |
|
5 | 5 | RSpec.describe OpenapiFirst::Test::Methods do |
6 | | - it 'can be included' do |
7 | | - minitest_class = Class.new(Minitest::Test) do |
| 6 | + it 'includes PlainHelpers when included' do |
| 7 | + test_class = Class.new do |
8 | 8 | include OpenapiFirst::Test::Methods |
9 | 9 | end |
10 | | - expect(minitest_class.included_modules).to include(OpenapiFirst::Test::MinitestHelpers) |
11 | | - |
12 | | - other_class = Class.new do |
13 | | - include OpenapiFirst::Test::Methods |
14 | | - end |
15 | | - expect(other_class.included_modules).to include(OpenapiFirst::Test::PlainHelpers) |
| 10 | + expect(test_class.included_modules).to include(OpenapiFirst::Test::PlainHelpers) |
16 | 11 | end |
17 | 12 |
|
18 | | - it 'adds an app method that wraps the app' do |
| 13 | + it 'raises OpenapiFirst::Error when assertion fails' do |
19 | 14 | OpenapiFirst::Test.register('./examples/openapi.yaml') |
20 | | - myapp = ->(_env) { Rack::Response.new('hello').finish } |
21 | | - minitest_class = Class.new(Minitest::Test) do |
22 | | - include OpenapiFirst::Test::Methods[myapp] |
| 15 | + test_class = Class.new do |
| 16 | + include OpenapiFirst::Test::Methods |
| 17 | + |
| 18 | + def last_request = Rack::Request.new(Rack::MockRequest.env_for('/')) |
| 19 | + def last_response = Rack::Response.new |
23 | 20 | end |
24 | | - expect(minitest_class.included_modules).to include(OpenapiFirst::Test::MinitestHelpers) |
25 | 21 |
|
26 | | - test_app = minitest_class.new(1).app |
27 | | - env = Rack::MockRequest.env_for('/') |
28 | | - expect(test_app.call(env)).to eq(Rack::Response.new('hello').finish) |
29 | | - expect(env[OpenapiFirst::REQUEST]).to be_valid |
| 22 | + expect do |
| 23 | + test_class.new.assert_api_conform(status: 444) |
| 24 | + end.to raise_error(OpenapiFirst::Error) |
30 | 25 | end |
31 | 26 |
|
32 | | - it 'adds an app method that wraps the app for a specific API' do |
33 | | - OpenapiFirst::Test.register('./examples/openapi.yaml', as: :v1) |
34 | | - myapp = ->(_env) { Rack::Response.new('hello').finish } |
35 | | - minitest_class = Class.new(Minitest::Test) do |
36 | | - include OpenapiFirst::Test::Methods[myapp, api: :v1] |
| 27 | + context 'with Minitest' do |
| 28 | + it 'includes MinitestHelpers when included' do |
| 29 | + minitest_class = Class.new(Minitest::Test) do |
| 30 | + include OpenapiFirst::Test::Methods |
| 31 | + end |
| 32 | + expect(minitest_class.included_modules).to include(OpenapiFirst::Test::MinitestHelpers) |
37 | 33 | end |
38 | | - expect(minitest_class.included_modules).to include(OpenapiFirst::Test::MinitestHelpers) |
39 | 34 |
|
40 | | - test_app = minitest_class.new(1).app |
41 | | - env = Rack::MockRequest.env_for('/') |
42 | | - expect(test_app.call(env)).to eq(Rack::Response.new('hello').finish) |
43 | | - expect(env[OpenapiFirst::REQUEST]).to be_valid |
44 | | - end |
| 35 | + it 'raises Minitest::Assertion when assertion fails' do |
| 36 | + OpenapiFirst::Test.register('./examples/openapi.yaml') |
| 37 | + minitest_class = Class.new(Minitest::Test) do |
| 38 | + include OpenapiFirst::Test::Methods |
45 | 39 |
|
46 | | - it 'does not add an app method if app is nil' do |
47 | | - OpenapiFirst::Test.register('./examples/openapi.yaml', as: :v1) |
48 | | - minitest_class = Class.new(Minitest::Test) do |
49 | | - include OpenapiFirst::Test::Methods[api: :v1] |
50 | | - end |
51 | | - expect(minitest_class.included_modules).to include(OpenapiFirst::Test::MinitestHelpers) |
| 40 | + def last_request = Rack::Request.new(Rack::MockRequest.env_for('/')) |
| 41 | + def last_response = Rack::Response.new |
| 42 | + end |
52 | 43 |
|
53 | | - expect(minitest_class.new(1).respond_to?(:app)).to eq(false) |
| 44 | + expect do |
| 45 | + minitest_class.new('hey').assert_api_conform(status: 444) |
| 46 | + end.to raise_error(Minitest::Assertion) |
| 47 | + end |
54 | 48 | end |
55 | 49 |
|
56 | | - it 'detects wrong response status for Minitest' do |
57 | | - OpenapiFirst::Test.register('./examples/openapi.yaml') |
58 | | - minitest_class = Class.new(Minitest::Test) do |
59 | | - include OpenapiFirst::Test::Methods |
| 50 | + context 'with [arguments]' do |
| 51 | + it 'adds an app method that wraps the default API' do |
| 52 | + OpenapiFirst::Test.register('./examples/openapi.yaml') |
| 53 | + myapp = ->(_env) { Rack::Response.new('hello').finish } |
60 | 54 |
|
61 | | - def last_request = Rack::Request.new(Rack::MockRequest.env_for('/')) |
62 | | - def last_response = Rack::Response.new |
| 55 | + minitest_class = Class.new(Minitest::Test) do |
| 56 | + include OpenapiFirst::Test::Methods[myapp] |
| 57 | + end |
| 58 | + |
| 59 | + expect(minitest_class.included_modules).to include(OpenapiFirst::Test::MinitestHelpers) |
| 60 | + test_app = minitest_class.new(1).app |
| 61 | + env = Rack::MockRequest.env_for('/') |
| 62 | + expect(test_app.call(env)).to eq(Rack::Response.new('hello').finish) |
| 63 | + expect(env[OpenapiFirst::REQUEST]).to be_valid |
63 | 64 | end |
64 | 65 |
|
65 | | - expect do |
66 | | - minitest_class.new('hey').assert_api_conform(status: 444) |
67 | | - end.to raise_error(Minitest::Assertion) |
68 | | - end |
| 66 | + it 'adds an app method that wraps the app for a specific API' do |
| 67 | + OpenapiFirst::Test.register('./examples/openapi.yaml', as: :v1) |
| 68 | + myapp = ->(_env) { Rack::Response.new('hello').finish } |
69 | 69 |
|
70 | | - it 'detects wrong response status for non Minitest' do |
71 | | - OpenapiFirst::Test.register('./examples/openapi.yaml') |
72 | | - other_test_class = Class.new do |
73 | | - include OpenapiFirst::Test::Methods |
| 70 | + minitest_class = Class.new(Minitest::Test) do |
| 71 | + include OpenapiFirst::Test::Methods[myapp, api: :v1] |
| 72 | + end |
74 | 73 |
|
75 | | - def last_request = Rack::Request.new(Rack::MockRequest.env_for('/')) |
76 | | - def last_response = Rack::Response.new |
| 74 | + expect(minitest_class.included_modules).to include(OpenapiFirst::Test::MinitestHelpers) |
| 75 | + test_app = minitest_class.new(1).app |
| 76 | + env = Rack::MockRequest.env_for('/') |
| 77 | + expect(test_app.call(env)).to eq(Rack::Response.new('hello').finish) |
| 78 | + expect(env[OpenapiFirst::REQUEST]).to be_valid |
77 | 79 | end |
78 | 80 |
|
79 | | - expect do |
80 | | - other_test_class.new.assert_api_conform(status: 444) |
81 | | - end.to raise_error(OpenapiFirst::Error) |
| 81 | + it 'does not add an app method if app is nil' do |
| 82 | + OpenapiFirst::Test.register('./examples/openapi.yaml', as: :v1) |
| 83 | + |
| 84 | + minitest_class = Class.new(Minitest::Test) do |
| 85 | + include OpenapiFirst::Test::Methods[api: :v1] |
| 86 | + end |
| 87 | + |
| 88 | + expect(minitest_class.included_modules).to include(OpenapiFirst::Test::MinitestHelpers) |
| 89 | + expect(minitest_class.new(1).respond_to?(:app)).to eq(false) |
| 90 | + end |
82 | 91 | end |
83 | 92 | end |
0 commit comments