rspec-rails 3.0.0.beta2 → 3.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +2 -1
- data/Capybara.md +1 -3
- data/Changelog.md +126 -58
- data/README.md +31 -4
- data/lib/generators/rspec/controller/templates/controller_spec.rb +1 -1
- data/lib/generators/rspec/controller/templates/view_spec.rb +1 -1
- data/lib/generators/rspec/feature/templates/feature_spec.rb +1 -1
- data/lib/generators/rspec/helper/templates/helper_spec.rb +1 -1
- data/lib/generators/rspec/install/templates/spec/spec_helper.rb.tt +21 -1
- data/lib/generators/rspec/integration/templates/request_spec.rb +1 -1
- data/lib/generators/rspec/mailer/templates/mailer_spec.rb +1 -1
- data/lib/generators/rspec/model/templates/model_spec.rb +1 -1
- data/lib/generators/rspec/observer/templates/observer_spec.rb +1 -1
- data/lib/generators/rspec/scaffold/templates/controller_spec.rb +11 -3
- data/lib/generators/rspec/scaffold/templates/edit_spec.rb +4 -3
- data/lib/generators/rspec/scaffold/templates/index_spec.rb +2 -2
- data/lib/generators/rspec/scaffold/templates/new_spec.rb +5 -4
- data/lib/generators/rspec/scaffold/templates/routing_spec.rb +1 -1
- data/lib/generators/rspec/scaffold/templates/show_spec.rb +2 -2
- data/lib/generators/rspec/view/templates/view_spec.rb +1 -1
- data/lib/rspec/rails.rb +1 -8
- data/lib/rspec/rails/configuration.rb +72 -0
- data/lib/rspec/rails/example.rb +0 -64
- data/lib/rspec/rails/example/controller_example_group.rb +16 -12
- data/lib/rspec/rails/example/feature_example_group.rb +0 -2
- data/lib/rspec/rails/example/helper_example_group.rb +0 -2
- data/lib/rspec/rails/example/mailer_example_group.rb +0 -1
- data/lib/rspec/rails/example/model_example_group.rb +0 -4
- data/lib/rspec/rails/example/request_example_group.rb +0 -2
- data/lib/rspec/rails/example/routing_example_group.rb +0 -2
- data/lib/rspec/rails/example/view_example_group.rb +1 -2
- data/lib/rspec/rails/extensions.rb +0 -1
- data/lib/rspec/rails/fixture_support.rb +0 -8
- data/lib/rspec/rails/matchers.rb +1 -0
- data/lib/rspec/rails/matchers/have_http_status.rb +355 -0
- data/lib/rspec/rails/matchers/routing_matchers.rb +1 -1
- data/lib/rspec/rails/tasks/rspec.rake +12 -5
- data/lib/rspec/rails/version.rb +1 -1
- data/lib/rspec/rails/view_rendering.rb +6 -34
- metadata +42 -316
- metadata.gz.sig +0 -0
- data/features/Generators.md +0 -25
- data/features/GettingStarted.md +0 -84
- data/features/README.md +0 -48
- data/features/RailsVersions.md +0 -4
- data/features/Transactions.md +0 -84
- data/features/Upgrade.md +0 -121
- data/features/controller_specs/Cookies.md +0 -57
- data/features/controller_specs/README.md +0 -45
- data/features/controller_specs/anonymous_controller.feature +0 -436
- data/features/controller_specs/bypass_rescue.feature +0 -75
- data/features/controller_specs/controller_spec.feature +0 -58
- data/features/controller_specs/engine_routes.feature +0 -51
- data/features/controller_specs/isolation_from_views.feature +0 -87
- data/features/controller_specs/render_views.feature +0 -114
- data/features/directory_structure.feature +0 -71
- data/features/feature_specs/feature_spec.feature +0 -35
- data/features/helper_specs/helper_spec.feature +0 -122
- data/features/mailer_specs/url_helpers.feature +0 -38
- data/features/matchers/README.md +0 -18
- data/features/matchers/new_record_matcher.feature +0 -41
- data/features/matchers/redirect_to_matcher.feature +0 -40
- data/features/matchers/relation_match_array.feature +0 -27
- data/features/matchers/render_template_matcher.feature +0 -49
- data/features/mocks/mock_model.feature +0 -147
- data/features/mocks/stub_model.feature +0 -58
- data/features/model_specs/README.md +0 -21
- data/features/model_specs/errors_on.feature +0 -51
- data/features/model_specs/records.feature +0 -27
- data/features/model_specs/transactional_examples.feature +0 -109
- data/features/request_specs/request_spec.feature +0 -49
- data/features/routing_specs/README.md +0 -16
- data/features/routing_specs/be_routable_matcher.feature +0 -80
- data/features/routing_specs/engine_routes.feature +0 -38
- data/features/routing_specs/named_routes.feature +0 -18
- data/features/routing_specs/route_to_matcher.feature +0 -90
- data/features/step_definitions/additional_cli_steps.rb +0 -4
- data/features/step_definitions/model_steps.rb +0 -3
- data/features/support/capybara.rb +0 -7
- data/features/support/env.rb +0 -53
- data/features/support/rails_versions.rb +0 -4
- data/features/support/rubinius.rb +0 -6
- data/features/view_specs/inferred_controller_path.feature +0 -45
- data/features/view_specs/stub_template.feature +0 -51
- data/features/view_specs/view_spec.feature +0 -206
- data/lib/rspec/rails/extensions/active_record/base.rb +0 -58
- data/lib/rspec/rails/mocks.rb +0 -272
- data/lib/rspec/rails/vendor/webrat.rb +0 -33
- data/spec/generators/rspec/controller/controller_generator_spec.rb +0 -97
- data/spec/generators/rspec/feature/feature_generator_spec.rb +0 -43
- data/spec/generators/rspec/helper/helper_generator_spec.rb +0 -30
- data/spec/generators/rspec/install/install_generator_spec.rb +0 -30
- data/spec/generators/rspec/integration/integration_generator_spec.rb +0 -32
- data/spec/generators/rspec/mailer/mailer_generator_spec.rb +0 -48
- data/spec/generators/rspec/model/model_generator_spec.rb +0 -52
- data/spec/generators/rspec/observer/observer_generator_spec.rb +0 -21
- data/spec/generators/rspec/scaffold/scaffold_generator_spec.rb +0 -138
- data/spec/generators/rspec/view/view_generator_spec.rb +0 -41
- data/spec/rspec/rails/assertion_adapter_spec.rb +0 -28
- data/spec/rspec/rails/assertion_delegator_spec.rb +0 -43
- data/spec/rspec/rails/configuration_spec.rb +0 -26
- data/spec/rspec/rails/deprecations_spec.rb +0 -18
- data/spec/rspec/rails/example/controller_example_group_spec.rb +0 -159
- data/spec/rspec/rails/example/feature_example_group_spec.rb +0 -56
- data/spec/rspec/rails/example/helper_example_group_spec.rb +0 -66
- data/spec/rspec/rails/example/mailer_example_group_spec.rb +0 -21
- data/spec/rspec/rails/example/model_example_group_spec.rb +0 -15
- data/spec/rspec/rails/example/request_example_group_spec.rb +0 -17
- data/spec/rspec/rails/example/routing_example_group_spec.rb +0 -32
- data/spec/rspec/rails/example/view_example_group_spec.rb +0 -235
- data/spec/rspec/rails/extensions/active_model/errors_on_spec.rb +0 -23
- data/spec/rspec/rails/extensions/active_record/base_spec.rb +0 -42
- data/spec/rspec/rails/fixture_support_spec.rb +0 -17
- data/spec/rspec/rails/matchers/be_a_new_spec.rb +0 -142
- data/spec/rspec/rails/matchers/be_new_record_spec.rb +0 -33
- data/spec/rspec/rails/matchers/be_routable_spec.rb +0 -41
- data/spec/rspec/rails/matchers/be_valid_spec.rb +0 -73
- data/spec/rspec/rails/matchers/has_spec.rb +0 -29
- data/spec/rspec/rails/matchers/have_rendered_spec.rb +0 -93
- data/spec/rspec/rails/matchers/redirect_to_spec.rb +0 -81
- data/spec/rspec/rails/matchers/relation_match_array_spec.rb +0 -31
- data/spec/rspec/rails/matchers/route_to_spec.rb +0 -151
- data/spec/rspec/rails/minitest_lifecycle_adapter_spec.rb +0 -31
- data/spec/rspec/rails/mocks/mock_model_spec.rb +0 -400
- data/spec/rspec/rails/mocks/stub_model_spec.rb +0 -154
- data/spec/rspec/rails/setup_and_teardown_adapter_spec.rb +0 -32
- data/spec/rspec/rails/view_rendering_spec.rb +0 -111
- data/spec/spec_helper.rb +0 -33
- data/spec/support/ar_classes.rb +0 -42
- data/spec/support/helpers.rb +0 -34
- data/spec/support/matchers.rb +0 -9
- data/spec/support/null_object.rb +0 -6
@@ -6,7 +6,6 @@ if defined?(ActionMailer)
|
|
6
6
|
include ActionMailer::TestCase::Behavior
|
7
7
|
|
8
8
|
included do
|
9
|
-
metadata[:type] = :mailer
|
10
9
|
include ::Rails.application.routes.url_helpers
|
11
10
|
options = ::Rails.configuration.action_mailer.default_url_options
|
12
11
|
options.each { |key, value| default_url_options[key] = value } if options
|
@@ -10,7 +10,7 @@ module RSpec::Rails
|
|
10
10
|
|
11
11
|
module ClassMethods
|
12
12
|
def _default_helper
|
13
|
-
base = metadata[:
|
13
|
+
base = metadata[:description].split('/')[0..-2].join('/')
|
14
14
|
(base.camelize + 'Helper').constantize if base
|
15
15
|
rescue NameError
|
16
16
|
nil
|
@@ -142,7 +142,6 @@ module RSpec::Rails
|
|
142
142
|
included do
|
143
143
|
include ExampleMethods
|
144
144
|
|
145
|
-
metadata[:type] = :view
|
146
145
|
helper(*_default_helpers)
|
147
146
|
|
148
147
|
before do
|
@@ -26,14 +26,6 @@ module RSpec
|
|
26
26
|
fixtures RSpec.configuration.global_fixtures if RSpec.configuration.global_fixtures
|
27
27
|
end
|
28
28
|
end
|
29
|
-
|
30
|
-
RSpec.configure do |c|
|
31
|
-
c.include RSpec::Rails::FixtureSupport
|
32
|
-
c.add_setting :use_transactional_fixtures, :alias_with => :use_transactional_examples
|
33
|
-
c.add_setting :use_instantiated_fixtures
|
34
|
-
c.add_setting :global_fixtures
|
35
|
-
c.add_setting :fixture_path
|
36
|
-
end
|
37
29
|
end
|
38
30
|
end
|
39
31
|
end
|
data/lib/rspec/rails/matchers.rb
CHANGED
@@ -0,0 +1,355 @@
|
|
1
|
+
# The following code inspired and modified from Rails' `assert_response`:
|
2
|
+
#
|
3
|
+
# https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/testing/assertions/response.rb#L22-L38
|
4
|
+
#
|
5
|
+
# Thank you to all the Rails devs who did the heavy lifting on this!
|
6
|
+
|
7
|
+
module RSpec::Rails::Matchers
|
8
|
+
# Namespace for various implementations of `have_http_status`.
|
9
|
+
#
|
10
|
+
# @api private
|
11
|
+
module HaveHttpStatus
|
12
|
+
# Instantiates an instance of the proper matcher based on the provided
|
13
|
+
# `target`.
|
14
|
+
#
|
15
|
+
# @param target [Object] expected http status or code
|
16
|
+
# @return response matcher instance
|
17
|
+
def self.matcher_for_status(target)
|
18
|
+
if GenericStatus.valid_statuses.include?(target)
|
19
|
+
GenericStatus.new(target)
|
20
|
+
elsif Symbol === target
|
21
|
+
SymbolicStatus.new(target)
|
22
|
+
else
|
23
|
+
NumericCode.new(target)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# @api private
|
28
|
+
# Conversion function to coerce the provided object into an
|
29
|
+
# `ActionDispatch::TestResponse`.
|
30
|
+
#
|
31
|
+
# @param obj [Object] object to convert to a response
|
32
|
+
# @return [ActionDispatch::TestResponse]
|
33
|
+
def as_test_response(obj)
|
34
|
+
if ::ActionDispatch::Response === obj
|
35
|
+
::ActionDispatch::TestResponse.from_response(obj)
|
36
|
+
elsif ::ActionDispatch::TestResponse === obj
|
37
|
+
obj
|
38
|
+
elsif obj.respond_to?(:status_code) && obj.respond_to?(:response_headers)
|
39
|
+
# Acts As Capybara Session
|
40
|
+
# Hack to support `Capybara::Session` without having to load Capybara or
|
41
|
+
# catch `NameError`s for the undefined constants
|
42
|
+
::ActionDispatch::TestResponse.new.tap{ |resp|
|
43
|
+
resp.status = obj.status_code
|
44
|
+
resp.headers = obj.response_headers
|
45
|
+
resp.body = obj.body
|
46
|
+
}
|
47
|
+
else
|
48
|
+
raise TypeError, "Invalid response type: #{obj}"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
module_function :as_test_response
|
52
|
+
|
53
|
+
# @return [String, nil] a formatted failure message if `@invalid_response`
|
54
|
+
# is present, `nil` otherwise
|
55
|
+
def invalid_response_type_message
|
56
|
+
if @invalid_response
|
57
|
+
"expected a response object, but an instance of " +
|
58
|
+
"#{@invalid_response.class} was received"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# @api private
|
63
|
+
# Provides an implementation for `have_http_status` matching against
|
64
|
+
# numeric http status codes.
|
65
|
+
#
|
66
|
+
# Not intended to be instantiated directly.
|
67
|
+
#
|
68
|
+
# @example
|
69
|
+
# expect(response).to have_http_status(404)
|
70
|
+
#
|
71
|
+
# @see RSpec::Rails::Matchers.have_http_status
|
72
|
+
class NumericCode < RSpec::Matchers::BuiltIn::BaseMatcher
|
73
|
+
include HaveHttpStatus
|
74
|
+
|
75
|
+
def initialize(code)
|
76
|
+
@expected = code.to_i
|
77
|
+
@actual = nil
|
78
|
+
@invalid_response = nil
|
79
|
+
end
|
80
|
+
|
81
|
+
# @param [Object] response object providing an http code to match
|
82
|
+
# @return [Boolean] `true` if the numeric code matched the `response` code
|
83
|
+
def matches?(response)
|
84
|
+
test_response = as_test_response(response)
|
85
|
+
@actual = test_response.response_code
|
86
|
+
expected == @actual
|
87
|
+
rescue TypeError => _ignored
|
88
|
+
@invalid_response = response
|
89
|
+
false
|
90
|
+
end
|
91
|
+
|
92
|
+
# @return [String]
|
93
|
+
def description
|
94
|
+
"respond with numeric status code #{expected}"
|
95
|
+
end
|
96
|
+
|
97
|
+
# @return [String] explaining why the match failed
|
98
|
+
def failure_message
|
99
|
+
invalid_response_type_message ||
|
100
|
+
"expected the response to have status code #{expected.inspect}" +
|
101
|
+
" but it was #{actual.inspect}"
|
102
|
+
end
|
103
|
+
|
104
|
+
# @return [String] explaining why the match failed
|
105
|
+
def failure_message_when_negated
|
106
|
+
invalid_response_type_message ||
|
107
|
+
"expected the response not to have status code #{expected.inspect}" +
|
108
|
+
" but it did"
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
# @api private
|
113
|
+
# Provides an implementation for `have_http_status` matching against
|
114
|
+
# Rack symbol http status codes.
|
115
|
+
#
|
116
|
+
# Not intended to be instantiated directly.
|
117
|
+
#
|
118
|
+
# @example
|
119
|
+
# expect(response).to have_http_status(:created)
|
120
|
+
#
|
121
|
+
# @see RSpec::Rails::Matchers.have_http_status
|
122
|
+
# @see https://github.com/rack/rack/blob/master/lib/rack/utils.rb `Rack::Utils::SYMBOL_TO_STATUS_CODE`
|
123
|
+
class SymbolicStatus < RSpec::Matchers::BuiltIn::BaseMatcher
|
124
|
+
include HaveHttpStatus
|
125
|
+
|
126
|
+
def initialize(status)
|
127
|
+
@expected_status = status
|
128
|
+
@actual = nil
|
129
|
+
@invalid_response = nil
|
130
|
+
unless set_expected_code!
|
131
|
+
raise ArgumentError, "Invalid HTTP status: #{status.inspect}"
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
# @param [Object] response object providing an http code to match
|
136
|
+
# @return [Boolean] `true` if Rack's associated numeric HTTP code matched
|
137
|
+
# the `response` code
|
138
|
+
def matches?(response)
|
139
|
+
test_response = as_test_response(response)
|
140
|
+
@actual = test_response.response_code
|
141
|
+
expected == @actual
|
142
|
+
rescue TypeError => _ignored
|
143
|
+
@invalid_response = response
|
144
|
+
false
|
145
|
+
end
|
146
|
+
|
147
|
+
# @return [String]
|
148
|
+
def description
|
149
|
+
"respond with status code #{pp_expected}"
|
150
|
+
end
|
151
|
+
|
152
|
+
# @return [String] explaining why the match failed
|
153
|
+
def failure_message
|
154
|
+
invalid_response_type_message ||
|
155
|
+
"expected the response to have status code #{pp_expected} but it" +
|
156
|
+
" was #{pp_actual}"
|
157
|
+
end
|
158
|
+
|
159
|
+
# @return [String] explaining why the match failed
|
160
|
+
def failure_message_when_negated
|
161
|
+
invalid_response_type_message ||
|
162
|
+
"expected the response not to have status code #{pp_expected} " +
|
163
|
+
"but it did"
|
164
|
+
end
|
165
|
+
|
166
|
+
# The initialized expected status symbol
|
167
|
+
attr_reader :expected_status
|
168
|
+
private :expected_status
|
169
|
+
|
170
|
+
private
|
171
|
+
|
172
|
+
# @return [Symbol] representing the actual http numeric code
|
173
|
+
def actual_status
|
174
|
+
if actual
|
175
|
+
@actual_status ||= compute_status_from(actual)
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
# Reverse lookup of the Rack status code symbol based on the numeric http
|
180
|
+
# code
|
181
|
+
#
|
182
|
+
# @param code [Fixnum] http status code to look up
|
183
|
+
# @return [Symbol] representing the http numeric code
|
184
|
+
def compute_status_from(code)
|
185
|
+
status, _ = Rack::Utils::SYMBOL_TO_STATUS_CODE.find{ |_, c| c == code }
|
186
|
+
status
|
187
|
+
end
|
188
|
+
|
189
|
+
# @return [String] pretty format the actual response status
|
190
|
+
def pp_actual
|
191
|
+
pp_status(actual_status, actual)
|
192
|
+
end
|
193
|
+
|
194
|
+
# @return [String] pretty format the expected status and associated code
|
195
|
+
def pp_expected
|
196
|
+
pp_status(expected_status, expected)
|
197
|
+
end
|
198
|
+
|
199
|
+
# @return [String] pretty format the actual response status
|
200
|
+
def pp_status(status, code)
|
201
|
+
if status
|
202
|
+
"#{status.inspect} (#{code})"
|
203
|
+
else
|
204
|
+
code.to_s
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
# Sets `expected` to the numeric http code based on the Rack
|
209
|
+
# `expected_status` status
|
210
|
+
#
|
211
|
+
# @see Rack::Utils::SYMBOL_TO_STATUS_CODE
|
212
|
+
# @return [nil] if an associated code could not be found
|
213
|
+
def set_expected_code!
|
214
|
+
@expected ||= Rack::Utils::SYMBOL_TO_STATUS_CODE[expected_status]
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
# @api private
|
219
|
+
# Provides an implementation for `have_http_status` matching against
|
220
|
+
# `ActionDispatch::TestResponse` http status category queries.
|
221
|
+
#
|
222
|
+
# Not intended to be instantiated directly.
|
223
|
+
#
|
224
|
+
# @example
|
225
|
+
# expect(response).to have_http_status(:success)
|
226
|
+
# expect(response).to have_http_status(:error)
|
227
|
+
# expect(response).to have_http_status(:missing)
|
228
|
+
# expect(response).to have_http_status(:redirect)
|
229
|
+
#
|
230
|
+
# @see RSpec::Rails::Matchers.have_http_status
|
231
|
+
# @see ActionDispatch::TestResponse
|
232
|
+
class GenericStatus < RSpec::Matchers::BuiltIn::BaseMatcher
|
233
|
+
include HaveHttpStatus
|
234
|
+
|
235
|
+
# @return [Array<Symbol>] of status codes which represent a HTTP status
|
236
|
+
# code "group"
|
237
|
+
# @see https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/testing/test_response.rb `ActionDispatch::TestResponse`
|
238
|
+
def self.valid_statuses
|
239
|
+
[:error, :success, :missing, :redirect]
|
240
|
+
end
|
241
|
+
|
242
|
+
def initialize(type)
|
243
|
+
unless self.class.valid_statuses.include?(type)
|
244
|
+
raise ArgumentError, "Invalid generic HTTP status: #{type.inspect}"
|
245
|
+
end
|
246
|
+
@expected = type
|
247
|
+
@actual = nil
|
248
|
+
@invalid_response = nil
|
249
|
+
end
|
250
|
+
|
251
|
+
# @return [Boolean] `true` if Rack's associated numeric HTTP code matched
|
252
|
+
# the `response` code
|
253
|
+
def matches?(response)
|
254
|
+
test_response = as_test_response(response)
|
255
|
+
@actual = test_response.response_code
|
256
|
+
test_response.send("#{expected}?")
|
257
|
+
rescue TypeError => _ignored
|
258
|
+
@invalid_response = response
|
259
|
+
false
|
260
|
+
end
|
261
|
+
|
262
|
+
# @return [String]
|
263
|
+
def description
|
264
|
+
"respond with #{type_message}"
|
265
|
+
end
|
266
|
+
|
267
|
+
# @return [String] explaining why the match failed
|
268
|
+
def failure_message
|
269
|
+
invalid_response_type_message ||
|
270
|
+
"expected the response to have #{type_message} but it was #{actual}"
|
271
|
+
end
|
272
|
+
|
273
|
+
# @return [String] explaining why the match failed
|
274
|
+
def failure_message_when_negated
|
275
|
+
invalid_response_type_message ||
|
276
|
+
"expected the response not to have #{type_message} but it was #{actual}"
|
277
|
+
end
|
278
|
+
|
279
|
+
private
|
280
|
+
|
281
|
+
# @return [String] formating the expected status and associated code(s)
|
282
|
+
def type_message
|
283
|
+
@type_message ||= (expected == :error ? "an error" : "a #{expected}") +
|
284
|
+
" status code (#{type_codes})"
|
285
|
+
end
|
286
|
+
|
287
|
+
# @return [String] formatting the associated code(s) for the various
|
288
|
+
# status code "groups"
|
289
|
+
# @see https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/testing/test_response.rb `ActionDispatch::TestResponse`
|
290
|
+
# @see https://github.com/rack/rack/blob/master/lib/rack/response.rb `Rack::Response`
|
291
|
+
def type_codes
|
292
|
+
# At the time of this commit the most recent version of
|
293
|
+
# `ActionDispatch::TestResponse` defines the following aliases:
|
294
|
+
#
|
295
|
+
# alias_method :success?, :successful?
|
296
|
+
# alias_method :missing?, :not_found?
|
297
|
+
# alias_method :redirect?, :redirection?
|
298
|
+
# alias_method :error?, :server_error?
|
299
|
+
#
|
300
|
+
# It's parent `ActionDispatch::Response` includes
|
301
|
+
# `Rack::Response::Helpers` which defines the aliased methods as:
|
302
|
+
#
|
303
|
+
# def successful?; status >= 200 && status < 300; end
|
304
|
+
# def redirection?; status >= 300 && status < 400; end
|
305
|
+
# def server_error?; status >= 500 && status < 600; end
|
306
|
+
# def not_found?; status == 404; end
|
307
|
+
#
|
308
|
+
# @see https://github.com/rails/rails/blob/ca200378/actionpack/lib/action_dispatch/testing/test_response.rb#L17-L27
|
309
|
+
# @see https://github.com/rails/rails/blob/ca200378/actionpack/lib/action_dispatch/http/response.rb#L74
|
310
|
+
# @see https://github.com/rack/rack/blob/ce4a3959/lib/rack/response.rb#L119-L122
|
311
|
+
@type_codes ||= case expected
|
312
|
+
when :error
|
313
|
+
"5xx"
|
314
|
+
when :success
|
315
|
+
"2xx"
|
316
|
+
when :missing
|
317
|
+
"404"
|
318
|
+
when :redirect
|
319
|
+
"3xx"
|
320
|
+
end
|
321
|
+
end
|
322
|
+
end
|
323
|
+
end
|
324
|
+
|
325
|
+
# @api public
|
326
|
+
# Passes if `response` has a matching HTTP status code.
|
327
|
+
#
|
328
|
+
# The following symbolic status codes are allowed:
|
329
|
+
#
|
330
|
+
# - `Rack::Utils::SYMBOL_TO_STATUS_CODE`
|
331
|
+
# - One of the defined `ActionDispatch::TestResponse` aliases:
|
332
|
+
# - `:error`
|
333
|
+
# - `:missing`
|
334
|
+
# - `:redirect`
|
335
|
+
# - `:success`
|
336
|
+
#
|
337
|
+
# @example Accepts numeric and symbol statuses
|
338
|
+
# expect(response).to have_http_status(404)
|
339
|
+
# expect(response).to have_http_status(:created)
|
340
|
+
# expect(response).to have_http_status(:success)
|
341
|
+
# expect(response).to have_http_status(:error)
|
342
|
+
# expect(response).to have_http_status(:missing)
|
343
|
+
# expect(response).to have_http_status(:redirect)
|
344
|
+
#
|
345
|
+
# @example Works with standard `response` objects and Capybara's `page`
|
346
|
+
# expect(response).to have_http_status(404)
|
347
|
+
# expect(page).to have_http_status(:created)
|
348
|
+
#
|
349
|
+
# @see https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/testing/test_response.rb `ActionDispatch::TestResponse`
|
350
|
+
# @see https://github.com/rack/rack/blob/master/lib/rack/utils.rb `Rack::Utils::SYMBOL_TO_STATUS_CODE`
|
351
|
+
def have_http_status(target)
|
352
|
+
raise ArgumentError, "Invalid HTTP status: nil" unless target
|
353
|
+
HaveHttpStatus.matcher_for_status(target)
|
354
|
+
end
|
355
|
+
end
|