rspec-rails 2.14.2 → 4.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- checksums.yaml.gz.sig +0 -0
- data/.document +1 -1
- data/.yardopts +4 -2
- data/Capybara.md +6 -57
- data/Changelog.md +715 -37
- data/{License.txt → LICENSE.md} +5 -2
- data/README.md +287 -362
- data/lib/generators/rspec/channel/channel_generator.rb +12 -0
- data/lib/generators/rspec/channel/templates/channel_spec.rb.erb +7 -0
- data/lib/generators/rspec/controller/controller_generator.rb +23 -5
- data/lib/generators/rspec/controller/templates/controller_spec.rb +5 -5
- data/lib/generators/rspec/controller/templates/request_spec.rb +19 -0
- data/lib/generators/rspec/controller/templates/routing_spec.rb +13 -0
- data/lib/generators/rspec/controller/templates/view_spec.rb +2 -2
- data/lib/generators/rspec/feature/feature_generator.rb +29 -0
- data/lib/generators/rspec/feature/templates/feature_singular_spec.rb +5 -0
- data/lib/generators/rspec/feature/templates/feature_spec.rb +5 -0
- data/lib/generators/rspec/generator/generator_generator.rb +24 -0
- data/lib/generators/rspec/generator/templates/generator_spec.rb +6 -0
- data/lib/generators/rspec/helper/helper_generator.rb +2 -1
- data/lib/generators/rspec/helper/templates/helper_spec.rb +2 -2
- data/lib/generators/rspec/install/install_generator.rb +44 -5
- data/lib/generators/rspec/install/templates/spec/rails_helper.rb +79 -0
- data/lib/generators/rspec/integration/integration_generator.rb +8 -13
- data/lib/generators/rspec/integration/templates/request_spec.rb +4 -9
- data/lib/generators/rspec/job/job_generator.rb +12 -0
- data/lib/generators/rspec/{observer/templates/observer_spec.rb → job/templates/job_spec.rb.erb} +2 -2
- data/lib/generators/rspec/mailbox/mailbox_generator.rb +14 -0
- data/lib/generators/rspec/mailbox/templates/mailbox_spec.rb.erb +7 -0
- data/lib/generators/rspec/mailer/mailer_generator.rb +9 -1
- data/lib/generators/rspec/mailer/templates/mailer_spec.rb +7 -7
- data/lib/generators/rspec/mailer/templates/preview.rb +13 -0
- data/lib/generators/rspec/model/model_generator.rb +21 -6
- data/lib/generators/rspec/model/templates/fixtures.yml +1 -1
- data/lib/generators/rspec/model/templates/model_spec.rb +2 -2
- data/lib/generators/rspec/request/request_generator.rb +10 -0
- data/lib/generators/rspec/scaffold/scaffold_generator.rb +88 -148
- data/lib/generators/rspec/scaffold/templates/api_controller_spec.rb +129 -0
- data/lib/generators/rspec/scaffold/templates/api_request_spec.rb +131 -0
- data/lib/generators/rspec/scaffold/templates/controller_spec.rb +105 -80
- data/lib/generators/rspec/scaffold/templates/edit_spec.rb +10 -14
- data/lib/generators/rspec/scaffold/templates/index_spec.rb +5 -12
- data/lib/generators/rspec/scaffold/templates/new_spec.rb +11 -15
- data/lib/generators/rspec/scaffold/templates/request_spec.rb +137 -0
- data/lib/generators/rspec/scaffold/templates/routing_spec.rb +19 -12
- data/lib/generators/rspec/scaffold/templates/show_spec.rb +5 -12
- data/lib/generators/rspec/system/system_generator.rb +26 -0
- data/lib/generators/rspec/system/templates/system_spec.rb +9 -0
- data/lib/generators/rspec/view/templates/view_spec.rb +2 -2
- data/lib/generators/rspec/view/view_generator.rb +3 -2
- data/lib/generators/rspec.rb +18 -10
- data/lib/rspec/rails/active_record.rb +25 -0
- data/lib/rspec/rails/adapters.rb +49 -47
- data/lib/rspec/rails/configuration.rb +194 -0
- data/lib/rspec/rails/example/channel_example_group.rb +93 -0
- data/lib/rspec/rails/example/controller_example_group.rb +188 -138
- data/lib/rspec/rails/example/feature_example_group.rb +43 -20
- data/lib/rspec/rails/example/helper_example_group.rb +28 -26
- data/lib/rspec/rails/example/job_example_group.rb +23 -0
- data/lib/rspec/rails/example/mailbox_example_group.rb +80 -0
- data/lib/rspec/rails/example/mailer_example_group.rb +30 -14
- data/lib/rspec/rails/example/model_example_group.rb +8 -7
- data/lib/rspec/rails/example/rails_example_group.rb +4 -2
- data/lib/rspec/rails/example/request_example_group.rb +23 -16
- data/lib/rspec/rails/example/routing_example_group.rb +49 -40
- data/lib/rspec/rails/example/system_example_group.rb +125 -0
- data/lib/rspec/rails/example/view_example_group.rb +178 -134
- data/lib/rspec/rails/example.rb +4 -33
- data/lib/rspec/rails/extensions/active_record/proxy.rb +5 -11
- data/lib/rspec/rails/extensions.rb +0 -1
- data/lib/rspec/rails/feature_check.rb +47 -0
- data/lib/rspec/rails/file_fixture_support.rb +17 -0
- data/lib/rspec/rails/fixture_file_upload_support.rb +35 -0
- data/lib/rspec/rails/fixture_support.rb +54 -23
- data/lib/rspec/rails/matchers/action_cable/have_broadcasted_to.rb +170 -0
- data/lib/rspec/rails/matchers/action_cable/have_streams.rb +58 -0
- data/lib/rspec/rails/matchers/action_cable.rb +65 -0
- data/lib/rspec/rails/matchers/action_mailbox.rb +64 -0
- data/lib/rspec/rails/matchers/active_job.rb +465 -0
- data/lib/rspec/rails/matchers/base_matcher.rb +179 -0
- data/lib/rspec/rails/matchers/be_a_new.rb +69 -62
- data/lib/rspec/rails/matchers/be_new_record.rb +24 -21
- data/lib/rspec/rails/matchers/be_valid.rb +42 -33
- data/lib/rspec/rails/matchers/have_enqueued_mail.rb +198 -0
- data/lib/rspec/rails/matchers/have_http_status.rb +385 -0
- data/lib/rspec/rails/matchers/have_rendered.rb +55 -31
- data/lib/rspec/rails/matchers/redirect_to.rb +30 -29
- data/lib/rspec/rails/matchers/relation_match_array.rb +2 -2
- data/lib/rspec/rails/matchers/routing_matchers.rb +107 -93
- data/lib/rspec/rails/matchers.rb +22 -14
- data/lib/rspec/rails/tasks/rspec.rake +8 -18
- data/lib/rspec/rails/vendor/capybara.rb +12 -11
- data/lib/rspec/rails/version.rb +3 -1
- data/lib/rspec/rails/view_assigns.rb +18 -18
- data/lib/rspec/rails/view_path_builder.rb +29 -0
- data/lib/rspec/rails/view_rendering.rb +100 -63
- data/lib/rspec/rails/view_spec_methods.rb +56 -0
- data/lib/rspec/rails.rb +10 -10
- data/lib/rspec-rails.rb +67 -3
- data.tar.gz.sig +0 -0
- metadata +121 -94
- metadata.gz.sig +0 -0
- data/lib/autotest/rails_rspec2.rb +0 -85
- data/lib/generators/rspec/install/templates/.rspec +0 -1
- data/lib/generators/rspec/install/templates/spec/spec_helper.rb.tt +0 -49
- data/lib/generators/rspec/observer/observer_generator.rb +0 -12
- data/lib/rspec/rails/extensions/active_record/base.rb +0 -58
- data/lib/rspec/rails/matchers/have_extension.rb +0 -36
- data/lib/rspec/rails/mocks.rb +0 -274
- data/lib/rspec/rails/module_inclusion.rb +0 -19
- data/lib/rspec/rails/vendor/webrat.rb +0 -33
@@ -0,0 +1,465 @@
|
|
1
|
+
require "active_job/base"
|
2
|
+
require "active_job/arguments"
|
3
|
+
|
4
|
+
module RSpec
|
5
|
+
module Rails
|
6
|
+
module Matchers
|
7
|
+
# Namespace for various implementations of ActiveJob features
|
8
|
+
#
|
9
|
+
# @api private
|
10
|
+
module ActiveJob
|
11
|
+
# rubocop: disable Metrics/ClassLength
|
12
|
+
# @private
|
13
|
+
class Base < RSpec::Rails::Matchers::BaseMatcher
|
14
|
+
def initialize
|
15
|
+
@args = []
|
16
|
+
@queue = nil
|
17
|
+
@at = nil
|
18
|
+
@block = proc { }
|
19
|
+
set_expected_number(:exactly, 1)
|
20
|
+
end
|
21
|
+
|
22
|
+
def with(*args, &block)
|
23
|
+
@args = args
|
24
|
+
@block = block if block.present?
|
25
|
+
self
|
26
|
+
end
|
27
|
+
|
28
|
+
def on_queue(queue)
|
29
|
+
@queue = queue.to_s
|
30
|
+
self
|
31
|
+
end
|
32
|
+
|
33
|
+
def at(time_or_date)
|
34
|
+
case time_or_date
|
35
|
+
when Time then @at = Time.at(time_or_date.to_f)
|
36
|
+
else
|
37
|
+
@at = time_or_date
|
38
|
+
end
|
39
|
+
self
|
40
|
+
end
|
41
|
+
|
42
|
+
def exactly(count)
|
43
|
+
set_expected_number(:exactly, count)
|
44
|
+
self
|
45
|
+
end
|
46
|
+
|
47
|
+
def at_least(count)
|
48
|
+
set_expected_number(:at_least, count)
|
49
|
+
self
|
50
|
+
end
|
51
|
+
|
52
|
+
def at_most(count)
|
53
|
+
set_expected_number(:at_most, count)
|
54
|
+
self
|
55
|
+
end
|
56
|
+
|
57
|
+
def times
|
58
|
+
self
|
59
|
+
end
|
60
|
+
|
61
|
+
def once
|
62
|
+
exactly(:once)
|
63
|
+
end
|
64
|
+
|
65
|
+
def twice
|
66
|
+
exactly(:twice)
|
67
|
+
end
|
68
|
+
|
69
|
+
def thrice
|
70
|
+
exactly(:thrice)
|
71
|
+
end
|
72
|
+
|
73
|
+
def failure_message
|
74
|
+
"expected to #{self.class::FAILURE_MESSAGE_EXPECTATION_ACTION} #{base_message}".tap do |msg|
|
75
|
+
if @unmatching_jobs.any?
|
76
|
+
msg << "\nQueued jobs:"
|
77
|
+
@unmatching_jobs.each do |job|
|
78
|
+
msg << "\n #{base_job_message(job)}"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def failure_message_when_negated
|
85
|
+
"expected not to #{self.class::FAILURE_MESSAGE_EXPECTATION_ACTION} #{base_message}"
|
86
|
+
end
|
87
|
+
|
88
|
+
def message_expectation_modifier
|
89
|
+
case @expectation_type
|
90
|
+
when :exactly then "exactly"
|
91
|
+
when :at_most then "at most"
|
92
|
+
when :at_least then "at least"
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def supports_block_expectations?
|
97
|
+
true
|
98
|
+
end
|
99
|
+
|
100
|
+
private
|
101
|
+
|
102
|
+
def check(jobs)
|
103
|
+
@matching_jobs, @unmatching_jobs = jobs.partition do |job|
|
104
|
+
if job_match?(job) && arguments_match?(job) && queue_match?(job) && at_match?(job)
|
105
|
+
args = deserialize_arguments(job)
|
106
|
+
@block.call(*args)
|
107
|
+
true
|
108
|
+
else
|
109
|
+
false
|
110
|
+
end
|
111
|
+
end
|
112
|
+
@matching_jobs_count = @matching_jobs.size
|
113
|
+
|
114
|
+
case @expectation_type
|
115
|
+
when :exactly then @expected_number == @matching_jobs_count
|
116
|
+
when :at_most then @expected_number >= @matching_jobs_count
|
117
|
+
when :at_least then @expected_number <= @matching_jobs_count
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def base_message
|
122
|
+
"#{message_expectation_modifier} #{@expected_number} jobs,".tap do |msg|
|
123
|
+
msg << " with #{@args}," if @args.any?
|
124
|
+
msg << " on queue #{@queue}," if @queue
|
125
|
+
msg << " at #{@at.inspect}," if @at
|
126
|
+
msg << " but #{self.class::MESSAGE_EXPECTATION_ACTION} #{@matching_jobs_count}"
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def base_job_message(job)
|
131
|
+
msg_parts = []
|
132
|
+
msg_parts << "with #{deserialize_arguments(job)}" if job[:args].any?
|
133
|
+
msg_parts << "on queue #{job[:queue]}" if job[:queue]
|
134
|
+
msg_parts << "at #{Time.at(job[:at])}" if job[:at]
|
135
|
+
|
136
|
+
"#{job[:job].name} job".tap do |msg|
|
137
|
+
msg << " #{msg_parts.join(', ')}" if msg_parts.any?
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
def job_match?(job)
|
142
|
+
@job ? @job == job[:job] : true
|
143
|
+
end
|
144
|
+
|
145
|
+
def arguments_match?(job)
|
146
|
+
if @args.any?
|
147
|
+
args = serialize_and_deserialize_arguments(@args)
|
148
|
+
deserialized_args = deserialize_arguments(job)
|
149
|
+
RSpec::Mocks::ArgumentListMatcher.new(*args).args_match?(*deserialized_args)
|
150
|
+
else
|
151
|
+
true
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
def queue_match?(job)
|
156
|
+
return true unless @queue
|
157
|
+
|
158
|
+
@queue == job[:queue]
|
159
|
+
end
|
160
|
+
|
161
|
+
def at_match?(job)
|
162
|
+
return true unless @at
|
163
|
+
return job[:at].nil? if @at == :no_wait
|
164
|
+
return false unless job[:at]
|
165
|
+
|
166
|
+
scheduled_at = Time.at(job[:at])
|
167
|
+
values_match?(@at, scheduled_at) || check_for_inprecise_value(scheduled_at)
|
168
|
+
end
|
169
|
+
|
170
|
+
def check_for_inprecise_value(scheduled_at)
|
171
|
+
return unless Time === @at && values_match?(@at.change(usec: 0), scheduled_at)
|
172
|
+
|
173
|
+
RSpec.warn_with((<<-WARNING).gsub(/^\s+\|/, '').chomp)
|
174
|
+
|[WARNING] Your expected `at(...)` value does not match the job scheduled_at value
|
175
|
+
|unless microseconds are removed. This precision error often occurs when checking
|
176
|
+
|values against `Time.current` / `Time.now` which have usec precision, but Rails
|
177
|
+
|uses `n.seconds.from_now` internally which has a usec count of `0`.
|
178
|
+
|
|
179
|
+
|Use `change(usec: 0)` to correct these values. For example:
|
180
|
+
|
|
181
|
+
|`Time.current.change(usec: 0)`
|
182
|
+
|
|
183
|
+
|Note: RSpec cannot do this for you because jobs can be scheduled with usec
|
184
|
+
|precision and we do not know wether it is on purpose or not.
|
185
|
+
|
|
186
|
+
|
|
187
|
+
WARNING
|
188
|
+
false
|
189
|
+
end
|
190
|
+
|
191
|
+
def set_expected_number(relativity, count)
|
192
|
+
@expectation_type = relativity
|
193
|
+
@expected_number = case count
|
194
|
+
when :once then 1
|
195
|
+
when :twice then 2
|
196
|
+
when :thrice then 3
|
197
|
+
else Integer(count)
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
def serialize_and_deserialize_arguments(args)
|
202
|
+
serialized = ::ActiveJob::Arguments.serialize(args)
|
203
|
+
::ActiveJob::Arguments.deserialize(serialized)
|
204
|
+
rescue ::ActiveJob::SerializationError
|
205
|
+
args
|
206
|
+
end
|
207
|
+
|
208
|
+
def deserialize_arguments(job)
|
209
|
+
::ActiveJob::Arguments.deserialize(job[:args])
|
210
|
+
rescue ::ActiveJob::DeserializationError
|
211
|
+
job[:args]
|
212
|
+
end
|
213
|
+
|
214
|
+
def queue_adapter
|
215
|
+
::ActiveJob::Base.queue_adapter
|
216
|
+
end
|
217
|
+
end
|
218
|
+
# rubocop: enable Metrics/ClassLength
|
219
|
+
|
220
|
+
# @private
|
221
|
+
class HaveEnqueuedJob < Base
|
222
|
+
FAILURE_MESSAGE_EXPECTATION_ACTION = 'enqueue'.freeze
|
223
|
+
MESSAGE_EXPECTATION_ACTION = 'enqueued'.freeze
|
224
|
+
|
225
|
+
def initialize(job)
|
226
|
+
super()
|
227
|
+
@job = job
|
228
|
+
end
|
229
|
+
|
230
|
+
def matches?(proc)
|
231
|
+
raise ArgumentError, "have_enqueued_job and enqueue_job only support block expectations" unless Proc === proc
|
232
|
+
|
233
|
+
original_enqueued_jobs_count = queue_adapter.enqueued_jobs.count
|
234
|
+
proc.call
|
235
|
+
in_block_jobs = queue_adapter.enqueued_jobs.drop(original_enqueued_jobs_count)
|
236
|
+
|
237
|
+
check(in_block_jobs)
|
238
|
+
end
|
239
|
+
|
240
|
+
def does_not_match?(proc)
|
241
|
+
set_expected_number(:at_least, 1)
|
242
|
+
|
243
|
+
!matches?(proc)
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
# @private
|
248
|
+
class HaveBeenEnqueued < Base
|
249
|
+
FAILURE_MESSAGE_EXPECTATION_ACTION = 'enqueue'.freeze
|
250
|
+
MESSAGE_EXPECTATION_ACTION = 'enqueued'.freeze
|
251
|
+
|
252
|
+
def matches?(job)
|
253
|
+
@job = job
|
254
|
+
check(queue_adapter.enqueued_jobs)
|
255
|
+
end
|
256
|
+
|
257
|
+
def does_not_match?(proc)
|
258
|
+
set_expected_number(:at_least, 1)
|
259
|
+
|
260
|
+
!matches?(proc)
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
# @private
|
265
|
+
class HavePerformedJob < Base
|
266
|
+
FAILURE_MESSAGE_EXPECTATION_ACTION = 'perform'.freeze
|
267
|
+
MESSAGE_EXPECTATION_ACTION = 'performed'.freeze
|
268
|
+
|
269
|
+
def initialize(job)
|
270
|
+
super()
|
271
|
+
@job = job
|
272
|
+
end
|
273
|
+
|
274
|
+
def matches?(proc)
|
275
|
+
raise ArgumentError, "have_performed_job only supports block expectations" unless Proc === proc
|
276
|
+
|
277
|
+
original_performed_jobs_count = queue_adapter.performed_jobs.count
|
278
|
+
proc.call
|
279
|
+
in_block_jobs = queue_adapter.performed_jobs.drop(original_performed_jobs_count)
|
280
|
+
|
281
|
+
check(in_block_jobs)
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
285
|
+
# @private
|
286
|
+
class HaveBeenPerformed < Base
|
287
|
+
FAILURE_MESSAGE_EXPECTATION_ACTION = 'perform'.freeze
|
288
|
+
MESSAGE_EXPECTATION_ACTION = 'performed'.freeze
|
289
|
+
|
290
|
+
def matches?(job)
|
291
|
+
@job = job
|
292
|
+
check(queue_adapter.performed_jobs)
|
293
|
+
end
|
294
|
+
end
|
295
|
+
end
|
296
|
+
|
297
|
+
# @api public
|
298
|
+
# Passes if a job has been enqueued inside block. May chain at_least, at_most or exactly to specify a number of times.
|
299
|
+
#
|
300
|
+
# @example
|
301
|
+
# expect {
|
302
|
+
# HeavyLiftingJob.perform_later
|
303
|
+
# }.to have_enqueued_job
|
304
|
+
#
|
305
|
+
# # Using alias
|
306
|
+
# expect {
|
307
|
+
# HeavyLiftingJob.perform_later
|
308
|
+
# }.to enqueue_job
|
309
|
+
#
|
310
|
+
# expect {
|
311
|
+
# HelloJob.perform_later
|
312
|
+
# HeavyLiftingJob.perform_later
|
313
|
+
# }.to have_enqueued_job(HelloJob).exactly(:once)
|
314
|
+
#
|
315
|
+
# expect {
|
316
|
+
# 3.times { HelloJob.perform_later }
|
317
|
+
# }.to have_enqueued_job(HelloJob).at_least(2).times
|
318
|
+
#
|
319
|
+
# expect {
|
320
|
+
# HelloJob.perform_later
|
321
|
+
# }.to have_enqueued_job(HelloJob).at_most(:twice)
|
322
|
+
#
|
323
|
+
# expect {
|
324
|
+
# HelloJob.perform_later
|
325
|
+
# HeavyLiftingJob.perform_later
|
326
|
+
# }.to have_enqueued_job(HelloJob).and have_enqueued_job(HeavyLiftingJob)
|
327
|
+
#
|
328
|
+
# expect {
|
329
|
+
# HelloJob.set(wait_until: Date.tomorrow.noon, queue: "low").perform_later(42)
|
330
|
+
# }.to have_enqueued_job.with(42).on_queue("low").at(Date.tomorrow.noon)
|
331
|
+
#
|
332
|
+
# expect {
|
333
|
+
# HelloJob.set(queue: "low").perform_later(42)
|
334
|
+
# }.to have_enqueued_job.with(42).on_queue("low").at(:no_wait)
|
335
|
+
#
|
336
|
+
# expect {
|
337
|
+
# HelloJob.perform_later('rspec_rails', 'rails', 42)
|
338
|
+
# }.to have_enqueued_job.with { |from, to, times|
|
339
|
+
# # Perform more complex argument matching using dynamic arguments
|
340
|
+
# expect(from).to include "_#{to}"
|
341
|
+
# }
|
342
|
+
def have_enqueued_job(job = nil)
|
343
|
+
check_active_job_adapter
|
344
|
+
ActiveJob::HaveEnqueuedJob.new(job)
|
345
|
+
end
|
346
|
+
alias_method :enqueue_job, :have_enqueued_job
|
347
|
+
|
348
|
+
# @api public
|
349
|
+
# Passes if a job has been enqueued. May chain at_least, at_most or exactly to specify a number of times.
|
350
|
+
#
|
351
|
+
# @example
|
352
|
+
# before { ActiveJob::Base.queue_adapter.enqueued_jobs.clear }
|
353
|
+
#
|
354
|
+
# HeavyLiftingJob.perform_later
|
355
|
+
# expect(HeavyLiftingJob).to have_been_enqueued
|
356
|
+
#
|
357
|
+
# HelloJob.perform_later
|
358
|
+
# HeavyLiftingJob.perform_later
|
359
|
+
# expect(HeavyLiftingJob).to have_been_enqueued.exactly(:once)
|
360
|
+
#
|
361
|
+
# 3.times { HelloJob.perform_later }
|
362
|
+
# expect(HelloJob).to have_been_enqueued.at_least(2).times
|
363
|
+
#
|
364
|
+
# HelloJob.perform_later
|
365
|
+
# expect(HelloJob).to enqueue_job(HelloJob).at_most(:twice)
|
366
|
+
#
|
367
|
+
# HelloJob.perform_later
|
368
|
+
# HeavyLiftingJob.perform_later
|
369
|
+
# expect(HelloJob).to have_been_enqueued
|
370
|
+
# expect(HeavyLiftingJob).to have_been_enqueued
|
371
|
+
#
|
372
|
+
# HelloJob.set(wait_until: Date.tomorrow.noon, queue: "low").perform_later(42)
|
373
|
+
# expect(HelloJob).to have_been_enqueued.with(42).on_queue("low").at(Date.tomorrow.noon)
|
374
|
+
#
|
375
|
+
# HelloJob.set(queue: "low").perform_later(42)
|
376
|
+
# expect(HelloJob).to have_been_enqueued.with(42).on_queue("low").at(:no_wait)
|
377
|
+
def have_been_enqueued
|
378
|
+
check_active_job_adapter
|
379
|
+
ActiveJob::HaveBeenEnqueued.new
|
380
|
+
end
|
381
|
+
|
382
|
+
# @api public
|
383
|
+
# Passes if a job has been performed inside block. May chain at_least, at_most or exactly to specify a number of times.
|
384
|
+
#
|
385
|
+
# @example
|
386
|
+
# expect {
|
387
|
+
# perform_jobs { HeavyLiftingJob.perform_later }
|
388
|
+
# }.to have_performed_job
|
389
|
+
#
|
390
|
+
# expect {
|
391
|
+
# perform_jobs {
|
392
|
+
# HelloJob.perform_later
|
393
|
+
# HeavyLiftingJob.perform_later
|
394
|
+
# }
|
395
|
+
# }.to have_performed_job(HelloJob).exactly(:once)
|
396
|
+
#
|
397
|
+
# expect {
|
398
|
+
# perform_jobs { 3.times { HelloJob.perform_later } }
|
399
|
+
# }.to have_performed_job(HelloJob).at_least(2).times
|
400
|
+
#
|
401
|
+
# expect {
|
402
|
+
# perform_jobs { HelloJob.perform_later }
|
403
|
+
# }.to have_performed_job(HelloJob).at_most(:twice)
|
404
|
+
#
|
405
|
+
# expect {
|
406
|
+
# perform_jobs {
|
407
|
+
# HelloJob.perform_later
|
408
|
+
# HeavyLiftingJob.perform_later
|
409
|
+
# }
|
410
|
+
# }.to have_performed_job(HelloJob).and have_performed_job(HeavyLiftingJob)
|
411
|
+
#
|
412
|
+
# expect {
|
413
|
+
# perform_jobs {
|
414
|
+
# HelloJob.set(wait_until: Date.tomorrow.noon, queue: "low").perform_later(42)
|
415
|
+
# }
|
416
|
+
# }.to have_performed_job.with(42).on_queue("low").at(Date.tomorrow.noon)
|
417
|
+
def have_performed_job(job = nil)
|
418
|
+
check_active_job_adapter
|
419
|
+
ActiveJob::HavePerformedJob.new(job)
|
420
|
+
end
|
421
|
+
alias_method :perform_job, :have_performed_job
|
422
|
+
|
423
|
+
# @api public
|
424
|
+
# Passes if a job has been performed. May chain at_least, at_most or exactly to specify a number of times.
|
425
|
+
#
|
426
|
+
# @example
|
427
|
+
# before do
|
428
|
+
# ActiveJob::Base.queue_adapter.performed_jobs.clear
|
429
|
+
# ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true
|
430
|
+
# ActiveJob::Base.queue_adapter.perform_enqueued_at_jobs = true
|
431
|
+
# end
|
432
|
+
#
|
433
|
+
# HeavyLiftingJob.perform_later
|
434
|
+
# expect(HeavyLiftingJob).to have_been_performed
|
435
|
+
#
|
436
|
+
# HelloJob.perform_later
|
437
|
+
# HeavyLiftingJob.perform_later
|
438
|
+
# expect(HeavyLiftingJob).to have_been_performed.exactly(:once)
|
439
|
+
#
|
440
|
+
# 3.times { HelloJob.perform_later }
|
441
|
+
# expect(HelloJob).to have_been_performed.at_least(2).times
|
442
|
+
#
|
443
|
+
# HelloJob.perform_later
|
444
|
+
# HeavyLiftingJob.perform_later
|
445
|
+
# expect(HelloJob).to have_been_performed
|
446
|
+
# expect(HeavyLiftingJob).to have_been_performed
|
447
|
+
#
|
448
|
+
# HelloJob.set(wait_until: Date.tomorrow.noon, queue: "low").perform_later(42)
|
449
|
+
# expect(HelloJob).to have_been_performed.with(42).on_queue("low").at(Date.tomorrow.noon)
|
450
|
+
def have_been_performed
|
451
|
+
check_active_job_adapter
|
452
|
+
ActiveJob::HaveBeenPerformed.new
|
453
|
+
end
|
454
|
+
|
455
|
+
private
|
456
|
+
|
457
|
+
# @private
|
458
|
+
def check_active_job_adapter
|
459
|
+
return if ::ActiveJob::QueueAdapters::TestAdapter === ::ActiveJob::Base.queue_adapter
|
460
|
+
|
461
|
+
raise StandardError, "To use ActiveJob matchers set `ActiveJob::Base.queue_adapter = :test`"
|
462
|
+
end
|
463
|
+
end
|
464
|
+
end
|
465
|
+
end
|
@@ -0,0 +1,179 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Rails
|
3
|
+
module Matchers
|
4
|
+
# @api private
|
5
|
+
#
|
6
|
+
# Base class to build matchers. Should not be instantiated directly.
|
7
|
+
class BaseMatcher
|
8
|
+
include RSpec::Matchers::Composable
|
9
|
+
|
10
|
+
# @api private
|
11
|
+
# Used to detect when no arg is passed to `initialize`.
|
12
|
+
# `nil` cannot be used because it's a valid value to pass.
|
13
|
+
UNDEFINED = Object.new.freeze
|
14
|
+
|
15
|
+
# @private
|
16
|
+
attr_reader :actual, :expected, :rescued_exception
|
17
|
+
|
18
|
+
# @private
|
19
|
+
attr_writer :matcher_name
|
20
|
+
|
21
|
+
def initialize(expected = UNDEFINED)
|
22
|
+
@expected = expected unless UNDEFINED.equal?(expected)
|
23
|
+
end
|
24
|
+
|
25
|
+
# @api private
|
26
|
+
# Indicates if the match is successful. Delegates to `match`, which
|
27
|
+
# should be defined on a subclass. Takes care of consistently
|
28
|
+
# initializing the `actual` attribute.
|
29
|
+
def matches?(actual)
|
30
|
+
@actual = actual
|
31
|
+
match(expected, actual)
|
32
|
+
end
|
33
|
+
|
34
|
+
# @api private
|
35
|
+
# Used to wrap a block of code that will indicate failure by
|
36
|
+
# raising one of the named exceptions.
|
37
|
+
#
|
38
|
+
# This is used by rspec-rails for some of its matchers that
|
39
|
+
# wrap rails' assertions.
|
40
|
+
def match_unless_raises(*exceptions)
|
41
|
+
exceptions.unshift Exception if exceptions.empty?
|
42
|
+
begin
|
43
|
+
yield
|
44
|
+
true
|
45
|
+
rescue *exceptions => @rescued_exception
|
46
|
+
false
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# @api private
|
51
|
+
# Generates a description using {RSpec::Matchers::EnglishPhrasing}.
|
52
|
+
# @return [String]
|
53
|
+
def description
|
54
|
+
desc = RSpec::Matchers::EnglishPhrasing.split_words(self.class.matcher_name)
|
55
|
+
desc << RSpec::Matchers::EnglishPhrasing.list(@expected) if defined?(@expected)
|
56
|
+
desc
|
57
|
+
end
|
58
|
+
|
59
|
+
# @api private
|
60
|
+
# Matchers are not diffable by default. Override this to make your
|
61
|
+
# subclass diffable.
|
62
|
+
def diffable?
|
63
|
+
false
|
64
|
+
end
|
65
|
+
|
66
|
+
# @api private
|
67
|
+
# Most matchers are value matchers (i.e. meant to work with `expect(value)`)
|
68
|
+
# rather than block matchers (i.e. meant to work with `expect { }`), so
|
69
|
+
# this defaults to false. Block matchers must override this to return true.
|
70
|
+
def supports_block_expectations?
|
71
|
+
false
|
72
|
+
end
|
73
|
+
|
74
|
+
# @api private
|
75
|
+
def expects_call_stack_jump?
|
76
|
+
false
|
77
|
+
end
|
78
|
+
|
79
|
+
# @private
|
80
|
+
def expected_formatted
|
81
|
+
RSpec::Support::ObjectFormatter.format(@expected)
|
82
|
+
end
|
83
|
+
|
84
|
+
# @private
|
85
|
+
def actual_formatted
|
86
|
+
RSpec::Support::ObjectFormatter.format(@actual)
|
87
|
+
end
|
88
|
+
|
89
|
+
# @private
|
90
|
+
def self.matcher_name
|
91
|
+
@matcher_name ||= underscore(name.split('::').last)
|
92
|
+
end
|
93
|
+
|
94
|
+
# @private
|
95
|
+
def matcher_name
|
96
|
+
if defined?(@matcher_name)
|
97
|
+
@matcher_name
|
98
|
+
else
|
99
|
+
self.class.matcher_name
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
# @private
|
104
|
+
# Borrowed from ActiveSupport.
|
105
|
+
def self.underscore(camel_cased_word)
|
106
|
+
word = camel_cased_word.to_s.dup
|
107
|
+
word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
108
|
+
word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
|
109
|
+
word.tr!('-', '_')
|
110
|
+
word.downcase!
|
111
|
+
word
|
112
|
+
end
|
113
|
+
private_class_method :underscore
|
114
|
+
|
115
|
+
private
|
116
|
+
|
117
|
+
def assert_ivars(*expected_ivars)
|
118
|
+
return unless (expected_ivars - present_ivars).any?
|
119
|
+
|
120
|
+
ivar_list = RSpec::Matchers::EnglishPhrasing.list(expected_ivars)
|
121
|
+
raise "#{self.class.name} needs to supply#{ivar_list}"
|
122
|
+
end
|
123
|
+
|
124
|
+
alias present_ivars instance_variables
|
125
|
+
|
126
|
+
# @private
|
127
|
+
module HashFormatting
|
128
|
+
# `{ :a => 5, :b => 2 }.inspect` produces:
|
129
|
+
#
|
130
|
+
# {:a=>5, :b=>2}
|
131
|
+
#
|
132
|
+
# ...but it looks much better as:
|
133
|
+
#
|
134
|
+
# {:a => 5, :b => 2}
|
135
|
+
#
|
136
|
+
# This is idempotent and safe to run on a string multiple times.
|
137
|
+
def improve_hash_formatting(inspect_string)
|
138
|
+
inspect_string.gsub(/(\S)=>(\S)/, '\1 => \2')
|
139
|
+
end
|
140
|
+
module_function :improve_hash_formatting
|
141
|
+
end
|
142
|
+
|
143
|
+
include HashFormatting
|
144
|
+
|
145
|
+
# @api private
|
146
|
+
# Provides default implementations of failure messages, based on the `description`.
|
147
|
+
module DefaultFailureMessages
|
148
|
+
# @api private
|
149
|
+
# Provides a good generic failure message. Based on `description`.
|
150
|
+
# When subclassing, if you are not satisfied with this failure message
|
151
|
+
# you often only need to override `description`.
|
152
|
+
# @return [String]
|
153
|
+
def failure_message
|
154
|
+
"expected #{description_of @actual} to #{description}".dup
|
155
|
+
end
|
156
|
+
|
157
|
+
# @api private
|
158
|
+
# Provides a good generic negative failure message. Based on `description`.
|
159
|
+
# When subclassing, if you are not satisfied with this failure message
|
160
|
+
# you often only need to override `description`.
|
161
|
+
# @return [String]
|
162
|
+
def failure_message_when_negated
|
163
|
+
"expected #{description_of @actual} not to #{description}".dup
|
164
|
+
end
|
165
|
+
|
166
|
+
# @private
|
167
|
+
def self.has_default_failure_messages?(matcher)
|
168
|
+
matcher.method(:failure_message).owner == self &&
|
169
|
+
matcher.method(:failure_message_when_negated).owner == self
|
170
|
+
rescue NameError
|
171
|
+
false
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
include DefaultFailureMessages
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|