lib/resque_spec/matchers.rb in resque_spec-0.5.0 vs lib/resque_spec/matchers.rb in resque_spec-0.5.1

- old
+ new

@@ -1,16 +1,37 @@ require 'rspec' -RSpec::Matchers.define :have_queued do |*expected_args| - match do |actual| - ResqueSpec.in_queue?(actual, *expected_args, :queue_name => @queue_name) +module InQueueHelper + def self.extended(klass) + klass.instance_eval do + chain :in do |queue_name| + self.queue_name = queue_name + end + end end - chain :in do |queue_name| - @queue_name = queue_name + private + + attr_accessor :queue_name + + def queue(actual) + if @queue_name + ResqueSpec.queue_by_name(@queue_name) + else + ResqueSpec.queue_for(actual) + end end +end + +RSpec::Matchers.define :have_queued do |*expected_args| + extend InQueueHelper + + match do |actual| + queue(actual).any? { |entry| entry[:klass].to_s == actual.to_s && entry[:args] == 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| @@ -21,18 +42,16 @@ "have queued arguments of [#{expected_args.join(', ')}]" end end RSpec::Matchers.define :have_queue_size_of do |size| + extend InQueueHelper + match do |actual| - (@queue ||= ResqueSpec.queue_for(actual)).size == size + queue(actual).size == size end - chain :in do |queue_name| - @queue = ResqueSpec.queues[queue_name] - end - failure_message_for_should do |actual| "expected that #{actual} would have #{size} entries queued, but got #{@queue.size} instead" end failure_message_for_should_not do |actual| @@ -44,11 +63,11 @@ end end RSpec::Matchers.define :have_scheduled do |*expected_args| match do |actual| - ResqueSpec.scheduled_anytime?(actual, *expected_args) + ResqueSpec.schedule_for(actual).any? { |entry| entry[:klass].to_s == actual.to_s && entry[:args] == expected_args } end failure_message_for_should do |actual| "expected that #{actual} would have [#{expected_args.join(', ')}] queued" end @@ -62,10 +81,12 @@ end end RSpec::Matchers.define :have_scheduled_at do |*expected_args| match do |actual| - ResqueSpec.scheduled?(actual, *expected_args) + time = expected_args.first + other_args = expected_args[1..-1] + ResqueSpec.schedule_for(actual).any? { |entry| entry[:klass].to_s == actual.to_s && entry[:time] == time && entry[:args] == other_args } end failure_message_for_should do |actual| "expected that #{actual} would have [#{expected_args.join(', ')}] queued" end