lib/resque_spec/matchers.rb in resque_spec-0.15.0 vs lib/resque_spec/matchers.rb in resque_spec-0.16.0

- old
+ new

@@ -1,10 +1,21 @@ require 'rspec/core' require 'rspec/expectations' require 'rspec/mocks' +module ArgsHelper + private + + def match_args(expected_args, args) + arg_list_matcher = RSpec::Mocks::ArgumentListMatcher.new(expected_args) + arg_list_matcher.args_match?(args) + end +end + module InQueueHelper + include ArgsHelper + def self.included(klass) klass.class_eval do attr_accessor :queue_name end end @@ -19,11 +30,10 @@ ResqueSpec.queue_by_name(@queue_name) else ResqueSpec.queue_for(actual) end end - end RSpec::Matchers.define :be_queued do |*expected_args| include InQueueHelper @@ -39,31 +49,32 @@ match do |actual| matched = queue(actual).select do |entry| klass = entry.fetch(:class) args = entry.fetch(:args) - klass.to_s == actual.to_s && expected_args == args + klass.to_s == actual.to_s && match_args(expected_args, args) end if @times matched.size == @times else matched.size > 0 end end - failure_message_for_should do |actual| + failure_message do |actual| "expected that #{actual} would be queued with [#{expected_args.join(', ')}]#{@times_info}" end - failure_message_for_should_not do |actual| + failure_message_when_negated do |actual| "expected that #{actual} would not be queued with [#{expected_args.join(', ')}]#{@times_info}" end description do "be queued with arguments of [#{expected_args.join(', ')}]#{@times_info}" end + end RSpec::Matchers.define :have_queued do |*expected_args| include InQueueHelper @@ -79,25 +90,25 @@ match do |actual| matched = queue(actual).select do |entry| klass = entry.fetch(:class) args = entry.fetch(:args) - klass.to_s == actual.to_s && expected_args == args + klass.to_s == actual.to_s && match_args(expected_args, args) end if @times matched.size == @times else matched.size > 0 end end - failure_message_for_should do |actual| + failure_message do |actual| "expected that #{actual} would have [#{expected_args.join(', ')}] queued#{@times_info}" end - failure_message_for_should_not do |actual| + failure_message_when_negated do |actual| "expected that #{actual} would not have [#{expected_args.join(', ')}] queued#{@times_info}" end description do "have queued arguments of [#{expected_args.join(', ')}]#{@times_info}" @@ -109,15 +120,15 @@ match do |actual| queue(actual).size == size end - failure_message_for_should do |actual| + failure_message do |actual| "expected that #{actual} would have #{size} entries queued, but got #{queue(actual).size} instead" end - failure_message_for_should_not do |actual| + failure_message_when_negated do |actual| "expected that #{actual} would not have #{size} entries queued, but got #{queue(actual).size} instead" end description do "have a queue size of #{size}" @@ -129,24 +140,26 @@ match do |actual| queue(actual).size >= size end - failure_message_for_should do |actual| + failure_message do |actual| "expected that #{actual} would have at least #{size} entries queued, but got #{queue(actual).size} instead" end - failure_message_for_should_not do |actual| + failure_message_when_negated do |actual| "expected that #{actual} would not have at least #{size} entries queued, but got #{queue(actual).size} instead" end description do "have a queue size of at least #{size}" end end module ScheduleQueueHelper + include ArgsHelper + def self.included(klass) klass.class_eval do attr_accessor :queue_name end end @@ -182,11 +195,11 @@ end match do |actual| schedule_queue_for(actual).any? do |entry| class_matches = entry[:class].to_s == actual.to_s - args_match = expected_args == entry[:args] + args_match = match_args(expected_args, entry[:args]) time_matches = if @time entry[:time] == @time elsif @interval entry[:time].to_i == (entry[:stored_at] + @interval).to_i @@ -196,15 +209,15 @@ class_matches && args_match && time_matches end end - failure_message_for_should do |actual| + failure_message do |actual| ["expected that #{actual} would have [#{expected_args.join(', ')}] scheduled", @time_info].join(' ') end - failure_message_for_should_not do |actual| + failure_message_when_negated do |actual| ["expected that #{actual} would not have [#{expected_args.join(', ')}] scheduled", @time_info].join(' ') end description do "have scheduled arguments" @@ -216,18 +229,18 @@ warn "DEPRECATION WARNING: have_scheduled_at(time, *args) is deprecated and will be removed in future. Please use have_scheduled(*args).at(time) instead." match do |actual| time = expected_args.first other_args = expected_args[1..-1] - schedule_queue_for(actual).any? { |entry| entry[:class].to_s == actual.to_s && entry[:time] == time && other_args == entry[:args] } + schedule_queue_for(actual).any? { |entry| entry[:class].to_s == actual.to_s && entry[:time] == time && match_args(other_args, entry[:args]) } end - failure_message_for_should do |actual| + failure_message do |actual| "expected that #{actual} would have [#{expected_args.join(', ')}] scheduled" end - failure_message_for_should_not do |actual| + failure_message_when_negated do |actual| "expected that #{actual} would not have [#{expected_args.join(', ')}] scheduled" end description do "have scheduled at the given time the arguments" @@ -239,15 +252,15 @@ match do |actual| schedule_queue_for(actual).size == size end - failure_message_for_should do |actual| + failure_message do |actual| "expected that #{actual} would have #{size} scheduled entries, but got #{schedule_queue_for(actual).size} instead" end - failure_message_for_should_not do |actual| + failure_message_when_negated do |actual| "expected that #{actual} would have #{size} scheduled entries." end description do "have schedule size of #{size}" @@ -259,14 +272,14 @@ match do |actual| schedule_queue_for(actual).size >= size end - failure_message_for_should do |actual| + failure_message do |actual| "expected that #{actual} would have at least #{size} scheduled entries, but got #{schedule_queue_for(actual).size} instead" end - failure_message_for_should_not do |actual| + failure_message_when_negated do |actual| "expected that #{actual} would have at least #{size} scheduled entries." end description do "have schedule size of #{size}"