rspec-sidekiq 3.1.0 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +19 -0
  3. data/LICENSE +12 -0
  4. data/README.md +197 -76
  5. data/lib/rspec/sidekiq/batch.rb +20 -2
  6. data/lib/rspec/sidekiq/configuration.rb +13 -2
  7. data/lib/rspec/sidekiq/matchers/base.rb +257 -0
  8. data/lib/rspec/sidekiq/matchers/be_delayed.rb +17 -3
  9. data/lib/rspec/sidekiq/matchers/enqueue_sidekiq_job.rb +87 -0
  10. data/lib/rspec/sidekiq/matchers/have_enqueued_sidekiq_job.rb +25 -0
  11. data/lib/rspec/sidekiq/matchers.rb +13 -8
  12. data/lib/rspec/sidekiq/sidekiq.rb +1 -1
  13. data/lib/rspec/sidekiq/version.rb +1 -1
  14. metadata +89 -39
  15. data/.gitattributes +0 -22
  16. data/.gitignore +0 -2
  17. data/.rspec +0 -4
  18. data/.simplecov +0 -5
  19. data/Gemfile +0 -9
  20. data/lib/rspec/sidekiq/matchers/have_enqueued_job.rb +0 -183
  21. data/rspec-sidekiq.gemspec +0 -39
  22. data/spec/rspec/sidekiq/batch_spec.rb +0 -83
  23. data/spec/rspec/sidekiq/helpers/retries_exhausted_spec.rb +0 -40
  24. data/spec/rspec/sidekiq/matchers/be_delayed_spec.rb +0 -238
  25. data/spec/rspec/sidekiq/matchers/be_expired_in_spec.rb +0 -57
  26. data/spec/rspec/sidekiq/matchers/be_processed_in_spec.rb +0 -114
  27. data/spec/rspec/sidekiq/matchers/be_retryable_spec.rb +0 -129
  28. data/spec/rspec/sidekiq/matchers/be_unique_spec.rb +0 -115
  29. data/spec/rspec/sidekiq/matchers/have_enqueued_job_spec.rb +0 -229
  30. data/spec/rspec/sidekiq/matchers/save_backtrace_spec.rb +0 -136
  31. data/spec/rspec/sidekiq/sidekiq_spec.rb +0 -15
  32. data/spec/spec_helper.rb +0 -29
  33. data/spec/support/factories.rb +0 -33
  34. data/spec/support/init.rb +0 -6
  35. data/spec/support/test_action_mailer.rb +0 -6
  36. data/spec/support/test_job.rb +0 -6
  37. data/spec/support/test_resource.rb +0 -10
  38. data/spec/support/test_worker.rb +0 -8
  39. data/spec/support/test_worker_alternative.rb +0 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9521e2acad8dae8dc1c47afeb1a3f4f1052f44d2d512ae29866548f5cefdbb61
4
- data.tar.gz: 4ce5c1b0a18cec884a1b023ae113f3e692afcc471c9e2dd067c762d6051a77d5
3
+ metadata.gz: fb94abca8e805aa88d9c84deb0ddb22746542845b48cde790c4da9ae58f48ca0
4
+ data.tar.gz: 646f1ee2cc023cd988f29b50250c6794cb7563a78c35a397f1f3290cbf9df97b
5
5
  SHA512:
6
- metadata.gz: e3501020bc4f14f5294b1cb67483c3018f608111e4c50ec74c193927e0dadf41ad7532e5c2a695f7d107cd435094eb59751ab23743f6b97a316059f8cc77ba7f
7
- data.tar.gz: 79003f53fa67a71b288a2de8d35bdc65aeb123e5dedf929e8012cff2e999aadaa5f3830d9d4dc33889ff4a270adbf9b1045a81e0b7372882b577e8d9553e8e9c
6
+ metadata.gz: 0d0b4a026d3e28f55e03076023b83eb11334cd2759514837f0fd37ce74d01279bf0aa53236e84185eb89e949ad429f21c40e09d1858c5a0beb622f5001617311
7
+ data.tar.gz: 42c83b796648b68262a689f6fabe441719347e9464505ca4ade74ebab9cad748e51896af223b441adda74fe9b6300cb9ecb1f1974843fa45cf225dcbefc996ca
data/CHANGES.md CHANGED
@@ -1,3 +1,22 @@
1
+ 4.0.0
2
+ ---
3
+ * [BREAKING] Dropped support for matching jobs on ActiveJob's private API args, (e.g. `_aj_globalid` and `_aj_ruby2_keywords`). `_aj_globalid` can be replaced with the object itself, e.g. `have_enqueued_sidekiq_job(user)`.
4
+ * [BREAKING] Dropped support for Ruby 2.6
5
+ * [BREAKING] Mocking Sidekiq::Batch is now _opt in_ (was opt-out) by default
6
+ * [BREAKING] Dropped the `have_enqueued_job` matcher (deprecated since v3)
7
+ * [BREAKING] Dropped support for Sidekiq <5
8
+ * [BREAKING] Dropped support for Rails 5
9
+ * Add support for Sidekiq 7 (#192)
10
+ * Add support for builtin argument matchers from rspec mocks for
11
+ `have_enqueued_sidekiq_job` (#200)
12
+ * Add `#on` to `have_enqueued_sidekiq_job` to support testing queue at enqueue time (#197)
13
+ * Add `enqueue_sidekiq_job` for block-syntax style expectations
14
+ * Clarified `have_enqueued_sidekiq_job` error message to make it clear that the "actual arguments" list is an array of argument-lists across all enqueued jobs. (#195)
15
+ * Fix `in` and `at` evaluation to match Sidekiq (#194)
16
+ * Fix `be_delayed` argument matcher (#196)
17
+ * Fix argument matching ignoring order (#200)
18
+ * Fix `be_delayed` time math when using `delay_for` (#198)
19
+
1
20
  3.1.0
2
21
  ---
3
22
  * Add support for latest ruby and Rails 5 (coding-bunny #156)
data/LICENSE CHANGED
@@ -1,5 +1,17 @@
1
1
  The MIT License (MIT)
2
2
 
3
+ Copyright (c) 2023 Will Spurgin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10
+
11
+ Elements of this software and associated documentation files are covered by the following license:
12
+
13
+ The MIT License (MIT)
14
+
3
15
  Copyright (c) 2014, 2015 Phil Ostler
4
16
 
5
17
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
data/README.md CHANGED
@@ -1,23 +1,9 @@
1
- **Welcome @packrat386 as new maintainer for `rspec-sidekiq`!**
2
-
3
- # RSpec for Sidekiq
1
+ **Welcome @wspurgin as new maintainer for `rspec-sidekiq`!**
4
2
 
5
3
  [![RubyGems][gem_version_badge]][ruby_gems]
6
- [![Code Climate][code_climate_badge]][code_climate]
7
- [![Travis CI][travis_ci_badge]][travis_ci]
8
- [![Coveralls][coveralls_badge]][coveralls]
9
- [![Gemnasium][gemnasium_badge]][gemnasium]
10
-
11
- ***Simple testing of Sidekiq jobs via a collection of matchers and helpers***
12
-
13
- [RubyGems][ruby_gems] |
14
- [Code Climate][code_climate] |
15
- [GitHub][github] |
16
- [Travis CI][travis_ci] |
17
- [Coveralls][coveralls] |
18
- [Gemnasium][gemnasium] |
19
- [RubyDoc][ruby_doc] |
20
- [Ruby Toolbox][ruby_toolbox]
4
+ [![Github Actions CI][github_actions_badge]][github_actions]
5
+
6
+ Simple testing of Sidekiq jobs via a collection of matchers and helpers.
21
7
 
22
8
  [Jump to Matchers &raquo;](#matchers) | [Jump to Helpers &raquo;](#helpers)
23
9
 
@@ -48,33 +34,143 @@ end
48
34
  ```
49
35
 
50
36
  ## Matchers
51
- * [be_delayed](#be_delayed)
37
+ * [enqueue_sidekiq_job](#enqueue_sidekiq_job)
38
+ * [have_enqueued_sidekiq_job](#have_enqueued_sidekiq_job)
52
39
  * [be_processed_in](#be_processed_in)
53
40
  * [be_retryable](#be_retryable)
54
41
  * [be_unique](#be_unique)
55
- * [have_enqueued_sidekiq_job](#have_enqueued_sidekiq_job)
42
+ * [be_delayed (_deprecated_)](#be_delayed)
43
+
44
+ ### enqueue_sidekiq_job
45
+
46
+ *Describes that the block should enqueue a job*. Optionally specify the
47
+ specific job class, arguments, timing, and other context
56
48
 
57
- ### be_delayed
58
- *Describes a method that should be invoked asynchronously (See [Sidekiq Delayed Extensions][sidekiq_wiki_delayed_extensions])*
59
49
  ```ruby
60
- Object.delay.is_nil? # delay
61
- expect(Object.method :is_nil?).to be_delayed
62
- Object.delay.is_a? Object # delay with argument
63
- expect(Object.method :is_a?).to be_delayed(Object)
50
+ # Basic
51
+ expect { AwesomeJob.perform_async }.to enqueue_sidekiq_job
64
52
 
65
- Object.delay_for(1.hour).is_nil? # delay for
66
- expect(Object.method :is_nil?).to be_delayed.for 1.hour
67
- Object.delay_for(1.hour).is_a? Object # delay for with argument
68
- expect(Object.method :is_a?).to be_delayed(Object).for 1.hour
53
+ # A specific job class
54
+ expect { AwesomeJob.perform_async }.to enqueue_sidekiq_job(AwesomeJob)
69
55
 
70
- Object.delay_until(1.hour.from_now).is_nil? # delay until
71
- expect(Object.method :is_nil?).to be_delayed.until 1.hour.from_now
72
- Object.delay_until(1.hour.from_now).is_a? Object # delay until with argument
73
- expect(Object.method :is_a?).to be_delayed(Object).until 1.hour.from_now
56
+ # with specific arguments
57
+ expect { AwesomeJob.perform_async "Awesome!" }.to enqueue_sidekiq_job.with("Awesome!")
74
58
 
75
- #Rails Mailer
76
- MyMailer.delay.some_mail
77
- expect(MyMailer.instance_method :some_mail).to be_delayed
59
+ # On a specific queue
60
+ expect { AwesomeJob.set(queue: "high").perform_async }.to enqueue_sidekiq_job.on("high")
61
+
62
+ # At a specific datetime
63
+ specific_time = 1.hour.from_now
64
+ expect { AwesomeJob.perform_at(specific_time) }.to enqueue_sidekiq_job.at(specific_time)
65
+
66
+ # In a specific interval (be mindful of freezing or managing time here)
67
+ freeze_time do
68
+ expect { AwesomeJob.perform_in(1.hour) }.to enqueue_sidekiq_job.in(1.hour)
69
+ end
70
+
71
+ # Combine and chain them as desired
72
+ expect { AwesomeJob.perform_at(specific_time, "Awesome!") }.to(
73
+ enqueue_sidekiq_job(AwesomeJob)
74
+ .with("Awesome!")
75
+ .on("default")
76
+ .at(specific_time)
77
+ )
78
+ ```
79
+
80
+ ### have_enqueued_sidekiq_job
81
+ *Describes that there should be an enqueued job with the **specified
82
+ arguments***
83
+
84
+ ```ruby
85
+ AwesomeJob.perform_async 'Awesome', true
86
+ # test with...
87
+ expect(AwesomeJob).to have_enqueued_sidekiq_job('Awesome', true)
88
+ ```
89
+
90
+ You can use the built-in RSpec args matchers too:
91
+ ```ruby
92
+ AwesomeJob.perform_async({"something" => "Awesome", "extra" => "stuff"})
93
+
94
+ # using built-in matchers from rspec-mocks:
95
+ expect(AwesomeJob).to have_enqueued_sidekiq_job(hash_including("something" => "Awesome"))
96
+ expect(AwesomeJob).to have_enqueued_sidekiq_job(any_args)
97
+ expect(AwesomeJob).to have_enqueued_sidekiq_job(hash_excluding("bad_stuff" => anything))
98
+ ```
99
+
100
+ #### Testing scheduled jobs
101
+
102
+ *Use chainable matchers `#at` and `#in`*
103
+
104
+ ```ruby
105
+ time = 5.minutes.from_now
106
+ AwesomeJob.perform_at time, 'Awesome', true
107
+ # test with...
108
+ expect(AwesomeJob).to have_enqueued_sidekiq_job('Awesome', true).at(time)
109
+ ```
110
+ ```ruby
111
+ AwesomeJob.perform_in 5.minutes, 'Awesome', true
112
+ # test with...
113
+ expect(AwesomeJob).to have_enqueued_sidekiq_job('Awesome', true).in(5.minutes)
114
+ ```
115
+
116
+ #### Testing queue set for job
117
+
118
+ Use the chainable `#on` matcher
119
+
120
+ ```ruby
121
+ class AwesomeJob
122
+ include Sidekiq::Job
123
+
124
+ sidekiq_options queue: :low
125
+ end
126
+
127
+ AwesomeJob.perform_async("a little awesome")
128
+
129
+ # test with..
130
+ expect(AwesomeJob).to have_enqueued_sidekiq_job("a little awesome").on("low")
131
+
132
+ # Setting the queue when enqueuing
133
+ AwesomeJob.set(queue: "high").perform_async("Very Awesome!")
134
+
135
+ expect(AwesomeJob).to have_enqueued_sidekiq_job("Very Awesome!").on("high")
136
+ ```
137
+
138
+ #### Testing ActiveMailer jobs
139
+
140
+ ```ruby
141
+ user = User.first
142
+ AwesomeActionMailer.invite(user, true).deliver_later
143
+
144
+ expect(Sidekiq::Worker).to have_enqueued_sidekiq_job(
145
+ "AwesomeActionMailer",
146
+ "invite",
147
+ "deliver_now",
148
+ user,
149
+ true
150
+ )
151
+ ```
152
+
153
+ #### Testing a job is _not_ enqueued
154
+
155
+ The negative case for `have_enqueued_sidekiq_job` is provided, but it's
156
+ important to remember that `have_enqueued_sidekiq_job` is an expectation that a
157
+ job is enqueued _with specific arguments_. In other words, passing no arguments
158
+ to `have_enqueued_sidekiq_job` is implicitly telling the matcher to look for
159
+ jobs _without_ arguments.
160
+
161
+ In short, unless you tell the matcher that _no_ jobs with _any_ arguments should be enqueued, you'll get the wrong result:
162
+
163
+ ```ruby
164
+ # example this is a test that we'd expect to fail
165
+ AwesomeJob.perform_async "Actually not awesome"
166
+
167
+ ### BAD - saying there shouldn't be a job enqueued _without_ args
168
+ expect(AwesomeJob).not_to have_enqueued_sidekiq_job
169
+ # => passes! 😱 Our job was enqueued _with_ args so no job exists without args.
170
+
171
+ ### Good
172
+ expect(AwesomeJob).not_to have_enqueued_sidekiq_job(any_args)
173
+ # => fails
78
174
  ```
79
175
 
80
176
  ### be_processed_in
@@ -108,7 +204,7 @@ sidekiq_options backtrace: 5
108
204
  # test with...
109
205
  expect(AwesomeJob).to save_backtrace # or
110
206
  it { is_expected.to save_backtrace }
111
- # ...or alternatively specifiy the number of lines that should be saved
207
+ # ...or alternatively specify the number of lines that should be saved
112
208
  expect(AwesomeJob).to save_backtrace 5 # or
113
209
  it { is_expected.to save_backtrace 5 }
114
210
  # ...or when it should not save the backtrace
@@ -136,34 +232,32 @@ it { is_expected.to be_expired_in 1.hour }
136
232
  it { is_expected.to_not be_expired_in 2.hours }
137
233
  ```
138
234
 
139
- ### have_enqueued_sidekiq_job
140
- *Describes that there should be an enqueued job with the specified arguments*
235
+ ### be_delayed
141
236
 
142
- **Note:** When using rspec-rails >= 3.4, use `have_enqueued_sidekiq_job` instead to
143
- prevent a name clash with rspec-rails' ActiveJob matcher.
237
+ **This matcher is deprecated**. Use of it with Sidekiq 7+ will raise an error.
238
+ Sidekiq 7 [dropped Delayed
239
+ Extensions](https://github.com/sidekiq/sidekiq/issues/5076).
144
240
 
241
+ *Describes a method that should be invoked asynchronously (See [Sidekiq Delayed Extensions][sidekiq_wiki_delayed_extensions])*
145
242
  ```ruby
146
- AwesomeJob.perform_async 'Awesome', true
147
- # test with...
148
- expect(AwesomeJob).to have_enqueued_sidekiq_job('Awesome', true)
243
+ Object.delay.is_nil? # delay
244
+ expect(Object.method :is_nil?).to be_delayed
245
+ Object.delay.is_a? Object # delay with argument
246
+ expect(Object.method :is_a?).to be_delayed(Object)
149
247
 
150
- # Code written with older versions of the gem may use the deprecated
151
- # have_enqueued_job matcher.
152
- expect(AwesomeJob).to have_enqueued_job('Awesome', true)
153
- ```
248
+ Object.delay_for(1.hour).is_nil? # delay for
249
+ expect(Object.method :is_nil?).to be_delayed.for 1.hour
250
+ Object.delay_for(1.hour).is_a? Object # delay for with argument
251
+ expect(Object.method :is_a?).to be_delayed(Object).for 1.hour
154
252
 
155
- #### Testing scheduled jobs
156
- *Use chainable matchers `#at` and `#in`*
157
- ```ruby
158
- time = 5.minutes.from_now
159
- Awesomejob.perform_at time, 'Awesome', true
160
- # test with...
161
- expect(AwesomeJob).to have_enqueued_sidekiq_job('Awesome', true).at(time)
162
- ```
163
- ```ruby
164
- Awesomejob.perform_in 5.minutes, 'Awesome', true
165
- # test with...
166
- expect(AwesomeJob).to have_enqueued_sidekiq_job('Awesome', true).in(5.minutes)
253
+ Object.delay_until(1.hour.from_now).is_nil? # delay until
254
+ expect(Object.method :is_nil?).to be_delayed.until 1.hour.from_now
255
+ Object.delay_until(1.hour.from_now).is_a? Object # delay until with argument
256
+ expect(Object.method :is_a?).to be_delayed(Object).until 1.hour.from_now
257
+
258
+ #Rails Mailer
259
+ MyMailer.delay.some_mail
260
+ expect(MyMailer.instance_method :some_mail).to be_delayed
167
261
  ```
168
262
 
169
263
  ## Example matcher usage
@@ -185,11 +279,38 @@ end
185
279
  ```
186
280
 
187
281
  ## Helpers
188
- * [Batches (Sidekiq Pro)](#batches)
282
+ * [Batches (Sidekiq Pro) _experimental_](#batches)
189
283
  * [`within_sidekiq_retries_exhausted_block`](#within_sidekiq_retries_exhausted_block)
190
284
 
191
285
  ### Batches
192
- If you are using Sidekiq Batches ([Sidekiq Pro feature][sidekiq_wiki_batches]), rspec-sidekiq replaces the implementation (using the NullObject pattern) enabling testing without a Redis instance. Mocha and RSpec stubbing is supported here.
286
+
287
+ If you are using Sidekiq Batches ([Sidekiq Pro feature][sidekiq_wiki_batches]),
288
+ You can *opt-in* with `stub_batches` to make `rspec-sidekiq` mock the
289
+ implementation (using a NullObject pattern). This enables testing without a
290
+ Redis instance. Mocha and RSpec stubbing is supported here.
291
+
292
+ :warning: **Caution**: Opting-in to this feature, while allowing you to test without
293
+ having Redis, _does not_ provide the exact API that `Sidekiq::Batch` does. As
294
+ such it can cause surprises.
295
+
296
+
297
+ ```ruby
298
+ RSpec.describe "Using mocked batches", stub_batches: true do
299
+ it "uses mocked batches" do
300
+ batch = Sidekiq::Batch.new
301
+ batch.jobs do
302
+ SomeJob.perform_async 123
303
+ end
304
+
305
+ expect(SomeJob).to have_enqueued_sidekiq_job
306
+
307
+ # Caution, the NullObject pattern means that the mocked Batch implementation
308
+ # responds to anything... even if it's not on the true `Sidekiq::Batch` API
309
+ # For example, the following fails
310
+ expect { batch.foobar! }.to raise_error(NoMethodError)
311
+ end
312
+ end
313
+ ```
193
314
 
194
315
  ### within_sidekiq_retries_exhausted_block
195
316
  ```ruby
@@ -203,9 +324,15 @@ FooClass.within_sidekiq_retries_exhausted_block {
203
324
  ```
204
325
 
205
326
  ## Testing
206
- ```bundle exec rspec spec```
327
+ ```
328
+ bundle exec rspec spec
329
+ ```
207
330
 
208
331
  ## Maintainers
332
+ * @wspurgin
333
+
334
+ ### Alumni
335
+
209
336
  * @packrat386
210
337
  * @philostler
211
338
 
@@ -214,20 +341,14 @@ Please do! If there's a feature missing that you'd love to see then get in on th
214
341
 
215
342
  Issues/Pull Requests/Comments all welcome...
216
343
 
217
- [code_climate]: https://codeclimate.com/github/philostler/rspec-sidekiq
218
- [code_climate_badge]: https://codeclimate.com/github/philostler/rspec-sidekiq.svg
219
- [coveralls]: https://coveralls.io/r/philostler/rspec-sidekiq
220
- [coveralls_badge]: https://img.shields.io/coveralls/philostler/rspec-sidekiq.svg?branch=develop
221
344
  [gem_version_badge]: https://badge.fury.io/rb/rspec-sidekiq.svg
222
- [gemnasium]: https://gemnasium.com/philostler/rspec-sidekiq
223
- [gemnasium_badge]: https://gemnasium.com/philostler/rspec-sidekiq.svg
224
- [github]: http://github.com/philostler/rspec-sidekiq
345
+ [github]: http://github.com/wspurgin/rspec-sidekiq
225
346
  [ruby_doc]: http://rubydoc.info/gems/rspec-sidekiq/frames
226
347
  [ruby_gems]: http://rubygems.org/gems/rspec-sidekiq
227
348
  [ruby_toolbox]: http://www.ruby-toolbox.com/projects/rspec-sidekiq
228
- [travis_ci]: http://travis-ci.org/philostler/rspec-sidekiq
229
- [travis_ci_badge]: https://travis-ci.org/philostler/rspec-sidekiq.svg?branch=develop
349
+ [github_actions]: https://github.com/wspurgin/rspec-sidekiq/actions
350
+ [github_actions_badge]: https://github.com/wspurgin/rspec-sidekiq/actions/workflows/main.yml/badge.svg
230
351
 
231
- [rspec_sidekiq_wiki_faq_&_troubleshooting]: https://github.com/philostler/rspec-sidekiq/wiki/FAQ-&-Troubleshooting
232
- [sidekiq_wiki_batches]: https://github.com/mperham/sidekiq/wiki/Batches
233
- [sidekiq_wiki_delayed_extensions]: https://github.com/mperham/sidekiq/wiki/Delayed-Extensions
352
+ [rspec_sidekiq_wiki_faq_&_troubleshooting]: https://github.com/wspurgin/rspec-sidekiq/wiki/FAQ-&-Troubleshooting
353
+ [sidekiq_wiki_batches]: https://github.com/sidekiq/sidekiq/wiki/Batches
354
+ [sidekiq_wiki_delayed_extensions]: https://github.com/sidekiq/sidekiq/wiki/Delayed-Extensions
@@ -9,6 +9,24 @@ if defined? Sidekiq::Batch
9
9
  end
10
10
  end
11
11
 
12
+ ##
13
+ # Sidekiq::Batch is a Sidekiq::Pro feature. However the general consensus is
14
+ # that, by defeault, you can't test without redis. RSpec::Sidekiq includes
15
+ # a "null object" pattern implementation to mock Batches. This will mock
16
+ # Sidekiq::Batch and prevent it from using Redis.
17
+ #
18
+ # This is _opt-in_ only feature.
19
+ #
20
+ # RSpec.describe "Using mocked batches", stub_batches: true do
21
+ # it "uses mocked batches" do
22
+ # batch = Sidekiq::Batch.new
23
+ # batch.jobs do
24
+ # SomeJob.perform_async 123
25
+ # end
26
+ #
27
+ # expect(SomeJob).to have_enqueued_sidekiq_job
28
+ # end
29
+ # end
12
30
  class NullBatch < NullObject
13
31
  attr_accessor :description
14
32
  attr_reader :bid
@@ -49,7 +67,7 @@ if defined? Sidekiq::Batch
49
67
  @callbacks.each do |event, callback, options|
50
68
  if event != :success || failures == 0
51
69
  case callback
52
- when Class
70
+ when Class
53
71
  callback.new.send("on_#{event}", self, options)
54
72
  when String
55
73
  klass, meth = callback.split('#')
@@ -71,7 +89,7 @@ if defined? Sidekiq::Batch
71
89
  # :nocov:
72
90
  RSpec.configure do |config|
73
91
  config.before(:each) do |example|
74
- next if example.metadata[:stub_batches] == false
92
+ next unless example.metadata[:stub_batches] == true
75
93
 
76
94
  if mocked_with_mocha?
77
95
  Sidekiq::Batch.stubs(:new) { RSpec::Sidekiq::NullBatch.new }
@@ -1,13 +1,24 @@
1
+ require "rubygems"
2
+
1
3
  module RSpec
2
4
  module Sidekiq
3
5
  class Configuration
4
- attr_accessor :clear_all_enqueued_jobs, :enable_terminal_colours, :warn_when_jobs_not_processed_by_sidekiq
6
+ attr_accessor :clear_all_enqueued_jobs,
7
+ :enable_terminal_colours,
8
+ :warn_when_jobs_not_processed_by_sidekiq
5
9
 
6
10
  def initialize
7
- @clear_all_enqueued_jobs = true
11
+ # Display settings defaults
8
12
  @enable_terminal_colours = true
13
+
14
+ # Functional settings defaults
15
+ @clear_all_enqueued_jobs = true
9
16
  @warn_when_jobs_not_processed_by_sidekiq = true
10
17
  end
18
+
19
+ def sidekiq_gte_7?
20
+ Gem::Version.new(::Sidekiq::VERSION) >= Gem::Version.new("7.0.0")
21
+ end
11
22
  end
12
23
  end
13
24
  end