Class: RSpec::Sidekiq::Matchers::Base Private

Inherits:
Object
  • Object
show all
Includes:
Matchers::Composable, Mocks::ArgumentMatchers
Defined in:
lib/rspec/sidekiq/matchers/base.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Direct Known Subclasses

EnqueueSidekiqJob, HaveEnqueuedSidekiqJob

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Base.



186
187
188
189
190
# File 'lib/rspec/sidekiq/matchers/base.rb', line 186

def initialize
  @expected_arguments = [any_args]
  @expected_options = {}
  set_expected_count :positive, 1
end

Instance Attribute Details

#actual_jobsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



184
185
186
# File 'lib/rspec/sidekiq/matchers/base.rb', line 184

def actual_jobs
  @actual_jobs
end

#expected_argumentsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



184
185
186
# File 'lib/rspec/sidekiq/matchers/base.rb', line 184

def expected_arguments
  @expected_arguments
end

#expected_countObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



184
185
186
# File 'lib/rspec/sidekiq/matchers/base.rb', line 184

def expected_count
  @expected_count
end

#expected_optionsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



184
185
186
# File 'lib/rspec/sidekiq/matchers/base.rb', line 184

def expected_options
  @expected_options
end

#klassObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



184
185
186
# File 'lib/rspec/sidekiq/matchers/base.rb', line 184

def klass
  @klass
end

Instance Method Details

#at(timestamp) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



197
198
199
200
# File 'lib/rspec/sidekiq/matchers/base.rb', line 197

def at(timestamp)
  @expected_options["at"] = timestamp.to_time.to_i
  self
end

#at_least(n) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



242
243
244
245
# File 'lib/rspec/sidekiq/matchers/base.rb', line 242

def at_least(n)
  set_expected_count :at_least, n
  self
end

#at_most(n) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



247
248
249
250
# File 'lib/rspec/sidekiq/matchers/base.rb', line 247

def at_most(n)
  set_expected_count :at_most, n
  self
end

#common_messageObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



327
328
329
# File 'lib/rspec/sidekiq/matchers/base.rb', line 327

def common_message
  "#{prefix_message} #{count_message} #{klass} #{expected_count.last == 1 ? "job" : "jobs"}"
end

#count_messageObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



335
336
337
338
339
340
341
342
343
344
# File 'lib/rspec/sidekiq/matchers/base.rb', line 335

def count_message
  case expected_count[0]
  when :positive
    "a"
  when :exactly
    expected_count[1]
  else
    "#{expected_count[0].to_s.gsub('_', ' ')} #{expected_count[1]}"
  end
end

#descriptionObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



289
290
291
# File 'lib/rspec/sidekiq/matchers/base.rb', line 289

def description
  "#{common_message} with arguments #{expected_arguments}"
end

#exactly(n) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



237
238
239
240
# File 'lib/rspec/sidekiq/matchers/base.rb', line 237

def exactly(n)
  set_expected_count :exactly, n
  self
end

#failure_messageObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/rspec/sidekiq/matchers/base.rb', line 293

def failure_message
  message = ["expected to #{common_message}"]
  if expected_arguments
    message << "  with arguments:"
    message << "    -#{formatted(expected_arguments)}"
  end

  if expected_options.any?
    message << "  with context:"
    message << "    -#{formatted(expected_options)}"
  end

  if actual_jobs.any?
    message << "but enqueued only jobs"
    if expected_arguments
      job_messages = actual_jobs.map do |job|
        base = ["  -JID:#{job.jid} with arguments:"]
        base << "    -#{formatted(job.args)}"
        if expected_options.any?
          base << "   with context: #{formatted(job.context)}"
        end

        base.join("\n")
      end

      message << job_messages.join("\n")
    end
  else
    message << "but enqueued 0 jobs"
  end

  message.join("\n")
end

#failure_message_when_negatedObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



346
347
348
349
350
351
# File 'lib/rspec/sidekiq/matchers/base.rb', line 346

def failure_message_when_negated
  message = ["expected not to #{common_message} but enqueued #{actual_jobs.count}"]
  message << "  arguments: #{expected_arguments}" if expected_arguments.any?
  message << "  options: #{expected_options}" if expected_options.any?
  message.join("\n")
end

#formatted(thing) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



353
354
355
# File 'lib/rspec/sidekiq/matchers/base.rb', line 353

def formatted(thing)
  RSpec::Support::ObjectFormatter.format(thing)
end

#immediatelyObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



207
208
209
210
# File 'lib/rspec/sidekiq/matchers/base.rb', line 207

def immediately
  @expected_options["at"] = nil
  self
end

#in(interval) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



202
203
204
205
# File 'lib/rspec/sidekiq/matchers/base.rb', line 202

def in(interval)
  @expected_options["at"] = (Time.now.to_f + interval.to_f).to_i
  self
end

#neverObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



217
218
219
220
# File 'lib/rspec/sidekiq/matchers/base.rb', line 217

def never
  set_expected_count :exactly, 0
  self
end

#normalize_arguments(args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/rspec/sidekiq/matchers/base.rb', line 357

def normalize_arguments(args)
  if args.is_a?(Array)
    args.map{ |x| normalize_arguments(x) }
  elsif args.is_a?(Hash)
    args.each_with_object({}) do |(key, value), hash|
      hash[key.to_s] = normalize_arguments(value)
    end
  elsif args.is_a?(Symbol)
    args.to_s
  else
    args
  end
end

#on(queue) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



212
213
214
215
# File 'lib/rspec/sidekiq/matchers/base.rb', line 212

def on(queue)
  @expected_options["queue"] = queue
  self
end

#onceObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



222
223
224
225
# File 'lib/rspec/sidekiq/matchers/base.rb', line 222

def once
  set_expected_count :exactly, 1
  self
end

#prefix_messageObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

  • (NotImplementedError)


331
332
333
# File 'lib/rspec/sidekiq/matchers/base.rb', line 331

def prefix_message
  raise NotImplementedError
end

#set_expected_count(relativity, n) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



277
278
279
280
281
282
283
284
285
286
287
# File 'lib/rspec/sidekiq/matchers/base.rb', line 277

def set_expected_count(relativity, n)
  n =
    case n
    when Integer then n
    when :once   then 1
    when :twice  then 2
    when :thrice then 3
    else raise ArgumentError, "Unsupported #{n} in '#{relativity} #{n}'. Use either an Integer, :once, :twice, or :thrice."
    end
  @expected_count = [relativity, n]
end

#thriceObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



232
233
234
235
# File 'lib/rspec/sidekiq/matchers/base.rb', line 232

def thrice
  set_expected_count :exactly, 3
  self
end

#timesObject Also known as: time

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



252
253
254
# File 'lib/rspec/sidekiq/matchers/base.rb', line 252

def times
  self
end

#twiceObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



227
228
229
230
# File 'lib/rspec/sidekiq/matchers/base.rb', line 227

def twice
  set_expected_count :exactly, 2
  self
end

#with(*expected_arguments) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



192
193
194
195
# File 'lib/rspec/sidekiq/matchers/base.rb', line 192

def with(*expected_arguments)
  @expected_arguments = normalize_arguments(expected_arguments)
  self
end

#with_context(**kwargs) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

  • (ArgumentError)


257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/rspec/sidekiq/matchers/base.rb', line 257

def with_context(**kwargs)
  raise ArgumentError, "Must specify keyword arguments to with_context" if kwargs.empty?

  # gather keys and compare against currently set expected_options
  # Someone could have accidentally used with_context and other
  # chainables with different expectations. Better to explicitly
  # inform loudly of clashes than let them overwrite silently
  normalized = normalize_arguments(kwargs)
  already_set = normalized.keys & @expected_options.keys
  if already_set.any?
    prettied = already_set.map { |key| "'#{key}'" }
    raise ArgumentError, "There are already expectations against #{prettied.join(",")}. Did you already call other context chainables like `on` or `at`?"
  end

  # We're good, no accidental overwrites of expectations
  @expected_options.merge!(normalized)

  self
end