Sha256: eae6f521c99a3153b93289d0557a308fa8684641e330badb321215ccaa705b89

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

require 'resque'

module ResqueSpec
  extend self

  def in_queue?(klass, *args)
    queue_for(klass).any? {|entry| entry[:klass] == klass && entry[:args] == args}
  end

  def queue_for(klass)
    queues[queue_name(klass)]
  end

  def queue_name(klass)
    queue_name = klass.instance_variable_get(:@queue) || klass.respond_to?(:queue) && klass.queue
    raise ::Resque::NoQueueError.new("Jobs must be placed onto a queue.") unless queue_name
  end

  def queues
    @queues ||= Hash.new {|h,k| h[k] = []}
  end

  def reset!
    queues.clear
  end

  module Resque
    def enqueue(klass, *args)
      ResqueSpec.queue_for(klass) << {:klass => klass, :args => args}
    end
  end
end

Resque.extend(ResqueSpec::Resque)

Spec::Matchers.define :have_queued do |*expected_args|
  match do |actual|
    ResqueSpec.in_queue?(actual, *expected_args)
  end

  failure_message_for_should do |actual|
    "expected that #{actual} would have [#{expected_args.join(', ')}] queued"
  end

  failure_message_for_should_not do |actual|
    "expected that #{actual} would not have [#{expected_args.join(', ')}] queued"
  end

  description do
    "have queued arguments"
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
resque_spec-0.2.0 lib/resque_spec/resque_spec.rb