@@ -24,6 +24,54 @@ def last_response = Rack::Response.new
2424 end . to raise_error ( OpenapiFirst ::Error )
2525 end
2626
27+ context 'with RSpec' do
28+ let ( :app ) do
29+ lambda do |_ |
30+ res = Rack ::Response . new ( JSON . generate ( hello : 'there' ) )
31+ res . content_type = 'application/json'
32+ res . finish
33+ end
34+ end
35+
36+ context 'with metadata' , api : :v1 do
37+ include OpenapiFirst ::Test ::Methods
38+ include Rack ::Test ::Methods
39+
40+ it 'targets that api when calling assert_api_conform' do
41+ expect do
42+ assert_api_conform ( status : 200 )
43+ end . to raise_error ( OpenapiFirst ::Test ::NotRegisteredError ) do |ex |
44+ expect ( ex . message ) . to start_with ( "API description ':v1' not found." )
45+ end
46+ end
47+ end
48+
49+ context 'with an [api:] option' , api : :v2 do
50+ include OpenapiFirst ::Test ::Methods [ api : :v1 ]
51+
52+ it 'targets the api from the argument when calling assert_api_conform' do
53+ expect do
54+ assert_api_conform ( status : 200 )
55+ end . to raise_error ( OpenapiFirst ::Test ::NotRegisteredError ) do |ex |
56+ expect ( ex . message ) . to start_with ( "API description ':v1' not found." )
57+ end
58+ end
59+ end
60+
61+ context 'with an [Application] argument and metadata' , api : :v2 do
62+ include OpenapiFirst ::Test ::Methods [ -> ( _ ) { Rack ::Response . new ( 'hey' ) . finish } ]
63+ include Rack ::Test ::Methods
64+
65+ it 'targets that api when calling the app' do
66+ OpenapiFirst ::Test . register ( './examples/openapi.yaml' , as : :v2 )
67+
68+ get ( '/' )
69+
70+ expect ( last_request . env [ OpenapiFirst ::REQUEST ] . operation_id ) . to eq ( 'example#root' )
71+ end
72+ end
73+ end
74+
2775 context 'with Minitest' do
2876 it 'includes MinitestHelpers when included' do
2977 minitest_class = Class . new ( Minitest ::Test ) do
@@ -78,6 +126,38 @@ def last_response = Rack::Response.new
78126 expect ( env [ OpenapiFirst ::REQUEST ] ) . to be_valid
79127 end
80128
129+ it 'adds an assert_api_conform method that targets the specified API' do
130+ OpenapiFirst ::Test . register ( './examples/openapi.yaml' , as : :v1 )
131+ test_class = Class . new do
132+ include OpenapiFirst ::Test ::Methods [ api : :v1 ]
133+
134+ def last_request = Rack ::Request . new ( Rack ::MockRequest . env_for ( '/' ) )
135+ def last_response = Rack ::Response . new
136+ end
137+
138+ expect ( test_class . new . openapi_first_default_api ) . to eq ( :v1 )
139+
140+ expect do
141+ test_class . new . assert_api_conform ( status : 444 )
142+ end . to raise_error ( OpenapiFirst ::Error )
143+ end
144+
145+ it 'adds an assert_api_conform method that still can target another API' do
146+ OpenapiFirst ::Test . register ( './examples/openapi.yaml' , as : :v1 )
147+ test_class = Class . new do
148+ include OpenapiFirst ::Test ::Methods [ api : :v1 ]
149+
150+ def last_request = Rack ::Request . new ( Rack ::MockRequest . env_for ( '/' ) )
151+ def last_response = Rack ::Response . new
152+ end
153+
154+ expect do
155+ test_class . new . assert_api_conform ( status : 444 , api : :other )
156+ end . to raise_error ( OpenapiFirst ::Test ::NotRegisteredError ) do |ex |
157+ expect ( ex . message ) . to start_with ( "API description ':other' not found." )
158+ end
159+ end
160+
81161 it 'does not add an app method if app is nil' do
82162 OpenapiFirst ::Test . register ( './examples/openapi.yaml' , as : :v1 )
83163
0 commit comments