@@ -243,7 +243,11 @@ class GenericStatus < RSpec::Matchers::BuiltIn::BaseMatcher
243
243
# code "group"
244
244
# @see https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/testing/test_response.rb `ActionDispatch::TestResponse`
245
245
def self . valid_statuses
246
- [ :error , :success , :missing , :redirect ]
246
+ [
247
+ :error , :success , :missing ,
248
+ :server_error , :successful , :not_found ,
249
+ :redirect
250
+ ]
247
251
end
248
252
249
253
def initialize ( type )
@@ -256,11 +260,11 @@ def initialize(type)
256
260
end
257
261
258
262
# @return [Boolean] `true` if Rack's associated numeric HTTP code matched
259
- # the `response` code
263
+ # the `response` code or the named response status
260
264
def matches? ( response )
261
265
test_response = as_test_response ( response )
262
266
@actual = test_response . response_code
263
- test_response . send ( " #{ expected } ?" )
267
+ check_expected_status ( test_response , expected )
264
268
rescue TypeError => _ignored
265
269
@invalid_response = response
266
270
false
@@ -283,6 +287,28 @@ def failure_message_when_negated
283
287
"expected the response not to have #{ type_message } but it was #{ actual } "
284
288
end
285
289
290
+ protected
291
+
292
+ if 5 < ::Rails ::VERSION ::MAJOR ||
293
+ ( ::Rails ::VERSION ::MAJOR == 5 && 2 <= ::Rails ::VERSION ::MINOR )
294
+ RESPONSE_METHODS = {
295
+ :success => 'successful' ,
296
+ :error => 'server_error' ,
297
+ :missing => 'not_found'
298
+ } . freeze
299
+ else
300
+ RESPONSE_METHODS = {
301
+ :successful => 'success' ,
302
+ :server_error => 'error' ,
303
+ :not_found => 'missing'
304
+ } . freeze
305
+ end
306
+
307
+ def check_expected_status ( test_response , expected )
308
+ test_response . send (
309
+ "#{ RESPONSE_METHODS . fetch ( expected , expected ) } ?" )
310
+ end
311
+
286
312
private
287
313
288
314
# @return [String] formating the expected status and associated code(s)
@@ -316,11 +342,11 @@ def type_codes
316
342
# @see https://github.com/rails/rails/blob/ca200378/actionpack/lib/action_dispatch/http/response.rb#L74
317
343
# @see https://github.com/rack/rack/blob/ce4a3959/lib/rack/response.rb#L119-L122
318
344
@type_codes ||= case expected
319
- when :error
345
+ when :error , :server_error
320
346
"5xx"
321
- when :success
347
+ when :success , :successful
322
348
"2xx"
323
- when :missing
349
+ when :missing , :not_found
324
350
"404"
325
351
when :redirect
326
352
"3xx"
0 commit comments