Sha256: 160d6dd764d881b68fd48278eea3c0c9be5ea29ce4df4a4a02fa37460026a54f

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

require 'rspec'
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)

RSpec::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.3.0 lib/resque_spec/resque_spec.rb