Module: ResqueUnit::Assertions

Defined in:
lib/resque_unit/assertions.rb

Overview

These are a group of assertions you can use in your unit tests to verify that your code is using Resque correctly.

Instance Method Summary collapse

Instance Method Details

#assert_job_created(queue_name, klass, args = nil, message = nil, &block) ⇒ Object

Asserts that a job was created and queued into the specified queue



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/resque_unit/assertions.rb', line 42

def assert_job_created(queue_name, klass, args = nil, message = nil, &block)
  queue = if block_given?
    snapshot = Resque.size(queue_name)
    yield
    Resque.all(queue_name)[snapshot..-1]
  else
    Resque.all(queue_name)
  end

  assert_with_custom_message(in_queue?(queue, klass, args),
    message || "#{klass}#{args ? " with #{args.inspect}" : ""} should have been queued in #{queue_name}: #{queue.inspect}.")
end

#assert_not_queued(klass = nil, args = nil, message = nil, &block) ⇒ Object

The opposite of assert_queued.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/resque_unit/assertions.rb', line 18

def assert_not_queued(klass = nil, args = nil, message = nil, &block)
  queue_name = Resque.queue_for(klass)

  queue = if block_given?
    snapshot = Resque.size(queue_name)
    yield
    Resque.all(queue_name)[snapshot..-1]
  else
    Resque.all(queue_name)
  end

  assert_with_custom_message(!in_queue?(queue, klass, args),
    message || "#{klass}#{args ? " with #{args.inspect}" : ""} should not have been queued in #{queue_name}.")
end

#assert_nothing_queued(message = nil, &block) ⇒ Object

Asserts no jobs were queued within the block passed.



34
35
36
37
38
39
# File 'lib/resque_unit/assertions.rb', line 34

def assert_nothing_queued(message = nil, &block)
  snapshot = Resque.size
  yield
  present = Resque.size
  assert_equal snapshot, present, message || "No jobs should have been queued"
end

#assert_queued(klass, args = nil, message = nil, &block) ⇒ Object Also known as: assert_queues

Asserts that klass has been queued into its appropriate queue at least once. If args is nil, it only asserts that the klass has been queued. Otherwise, it asserts that the klass has been queued with the correct arguments. Pass an empty array for args if you want to assert that klass has been queued without arguments. Pass a block if you want to assert something was queued within its execution.



11
12
13
14
# File 'lib/resque_unit/assertions.rb', line 11

def assert_queued(klass, args = nil, message = nil, &block)
  queue_name = Resque.queue_for(klass)
  assert_job_created(queue_name, klass, args, message, &block)
end