From cc616f6eb8a5d52cd4330c6cbb4bad4a094a49a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tonatiuh=20N=C3=BA=C3=B1ez?= Date: Sun, 19 Apr 2015 09:16:08 -0500 Subject: [PATCH] Extend "be_enqueued", "have_enqueued" and "have_scheduled" matchers This commit is for issue 97. So that when test fails with any of those matchers information will be shown about the matching queue (if it has jobs and expected jobs in the test is no more than 1) --- lib/resque_spec/matchers.rb | 45 ++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/lib/resque_spec/matchers.rb b/lib/resque_spec/matchers.rb index 331736b..967af2f 100644 --- a/lib/resque_spec/matchers.rb +++ b/lib/resque_spec/matchers.rb @@ -61,8 +61,16 @@ def queue(actual) end end + def actual_queue_message + if (!@times || @times == 1) && !(actual_queue = queue(actual)).empty? + actual_queue_args_str = actual_queue.last[:args].join(', ') + + ", but got #{actual} with [#{actual_queue_args_str}]#{@times_info} instead" + end + end + failure_message do |actual| - "expected that #{actual} would be queued with [#{expected_args.join(', ')}]#{@times_info}" + "expected that #{actual} would be queued with [#{expected_args.join(', ')}]#{@times_info}#{actual_queue_message}" end failure_message_when_negated do |actual| @@ -102,8 +110,16 @@ def queue(actual) end end + def actual_queue_message + if (!@times || @times == 1) && !(actual_queue = queue(actual)).empty? + actual_queue_args_str = actual_queue.last[:args].join(', ') + + ", but got #{actual} with [#{actual_queue_args_str}]#{@times_info} instead" + end + end + failure_message do |actual| - "expected that #{actual} would have [#{expected_args.join(', ')}] queued#{@times_info}" + "expected that #{actual} would have [#{expected_args.join(', ')}] queued#{@times_info}#{actual_queue_message}" end failure_message_when_negated do |actual| @@ -185,13 +201,13 @@ def schedule_queue_for(actual) chain :at do |timestamp| @interval = nil @time = timestamp - @time_info = "at #{@time}" + @time_info = " at #{@time}" end chain :in do |interval| @time = nil @interval = interval - @time_info = "in #{@interval} seconds" + @time_info = " in #{@interval} seconds" end match do |actual| @@ -211,12 +227,29 @@ def schedule_queue_for(actual) end end + def actual_queue_time_info(actual_queue) + if @time + " at #{actual_queue[:time]}" + elsif @interval + seconds = (actual_queue[:time] - actual_queue[:stored_at]).round + " in #{seconds} seconds" + end + end + + def actual_queue_message + if !(actual_queue = schedule_queue_for(actual)).empty? + actual_queue_args_str = actual_queue.last[:args].join(', ') + + ", but got #{actual} with [#{actual_queue_args_str}] scheduled#{actual_queue_time_info(actual_queue.last)} instead" + end + end + failure_message do |actual| - ["expected that #{actual} would have [#{expected_args.join(', ')}] scheduled", @time_info].join(' ') + "expected that #{actual} would have [#{expected_args.join(', ')}] scheduled#{@time_info}#{actual_queue_message}" end failure_message_when_negated do |actual| - ["expected that #{actual} would not have [#{expected_args.join(', ')}] scheduled", @time_info].join(' ') + "expected that #{actual} would not have [#{expected_args.join(', ')}] scheduled#{@time_info}" end description do