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

Inherits:
Object
  • Object
show all
Includes:
Matchers::Composable, Mocks::ArgumentMatchers, ArgumentNormalization, CountExpectation
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.



254
255
256
257
258
# File 'lib/rspec/sidekiq/matchers/base.rb', line 254

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.



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

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.



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

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.



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

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.



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

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.



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

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.



265
266
267
268
# File 'lib/rspec/sidekiq/matchers/base.rb', line 265

def at(timestamp)
  @expected_options["at"] = matcher?(timestamp) ? timestamp : 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.



310
311
312
313
# File 'lib/rspec/sidekiq/matchers/base.rb', line 310

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.



315
316
317
318
# File 'lib/rspec/sidekiq/matchers/base.rb', line 315

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.



383
384
385
# File 'lib/rspec/sidekiq/matchers/base.rb', line 383

def common_message
  "#{prefix_message} #{count_message} #{klass} #{expected_count.last == 1 ? "job" : "jobs"}"
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.



345
346
347
# File 'lib/rspec/sidekiq/matchers/base.rb', line 345

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.



305
306
307
308
# File 'lib/rspec/sidekiq/matchers/base.rb', line 305

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.



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/rspec/sidekiq/matchers/base.rb', line 349

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.



391
392
393
394
395
396
# File 'lib/rspec/sidekiq/matchers/base.rb', line 391

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.



398
399
400
# File 'lib/rspec/sidekiq/matchers/base.rb', line 398

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.



275
276
277
278
# File 'lib/rspec/sidekiq/matchers/base.rb', line 275

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.



270
271
272
273
# File 'lib/rspec/sidekiq/matchers/base.rb', line 270

def in(interval)
  @expected_options["at"] = matcher?(interval) ? interval : (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.



285
286
287
288
# File 'lib/rspec/sidekiq/matchers/base.rb', line 285

def never
  set_expected_count :exactly, 0
  self
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.



280
281
282
283
# File 'lib/rspec/sidekiq/matchers/base.rb', line 280

def on(queue)
  @expected_options["queue"] = normalize_arguments(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.



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

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)


387
388
389
# File 'lib/rspec/sidekiq/matchers/base.rb', line 387

def prefix_message
  raise NotImplementedError
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.



300
301
302
303
# File 'lib/rspec/sidekiq/matchers/base.rb', line 300

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.



320
321
322
# File 'lib/rspec/sidekiq/matchers/base.rb', line 320

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.



295
296
297
298
# File 'lib/rspec/sidekiq/matchers/base.rb', line 295

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.



260
261
262
263
# File 'lib/rspec/sidekiq/matchers/base.rb', line 260

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)


325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/rspec/sidekiq/matchers/base.rb', line 325

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