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.



180
181
182
183
184
# File 'lib/rspec/sidekiq/matchers/base.rb', line 180

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.



178
179
180
# File 'lib/rspec/sidekiq/matchers/base.rb', line 178

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.



178
179
180
# File 'lib/rspec/sidekiq/matchers/base.rb', line 178

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.



178
179
180
# File 'lib/rspec/sidekiq/matchers/base.rb', line 178

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.



178
179
180
# File 'lib/rspec/sidekiq/matchers/base.rb', line 178

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.



178
179
180
# File 'lib/rspec/sidekiq/matchers/base.rb', line 178

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.



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

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.



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

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.



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

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.



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

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.



304
305
306
307
308
309
310
311
312
313
# File 'lib/rspec/sidekiq/matchers/base.rb', line 304

def count_message
  case expected_count
  in [:positive, _]
    "a"
  in [:exactly, n]
    n
  in [relativity, n]
    "#{relativity.to_s.gsub('_', ' ')} #{n}"
  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.



258
259
260
# File 'lib/rspec/sidekiq/matchers/base.rb', line 258

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.



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

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.



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/rspec/sidekiq/matchers/base.rb', line 262

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 << "\n    -#{formatted(job.args)}"
        if expected_options.any?
          base << "\n   with context: #{formatted(job.context)}"
        end

        base
      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.



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

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.



322
323
324
# File 'lib/rspec/sidekiq/matchers/base.rb', line 322

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.



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

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.



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

def in(interval)
  @expected_options["at"] = (Time.now.to_f + interval.to_f).to_i
  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.



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

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.



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

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.



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

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)


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

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.



246
247
248
249
250
251
252
253
254
255
256
# File 'lib/rspec/sidekiq/matchers/base.rb', line 246

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.



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

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.



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

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.



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

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.



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

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