From 14243dbaf6149d68039035c449ff84d7213fa681 Mon Sep 17 00:00:00 2001 From: Kensuke Nagae Date: Mon, 11 Nov 2013 12:24:36 +0900 Subject: [PATCH 1/3] Show failure message with actual arguments --- lib/resque_spec/matchers.rb | 44 ++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/lib/resque_spec/matchers.rb b/lib/resque_spec/matchers.rb index 0612fc1..580d5d3 100644 --- a/lib/resque_spec/matchers.rb +++ b/lib/resque_spec/matchers.rb @@ -24,6 +24,22 @@ def queue(actual) end end + def actual_args(actual) + if queue(actual).first + queue(actual).first[:args] + else + [] + end + end + + def actual_args_str(actual) + if actual_args(actual).empty? + 'no args' + else + actual_args(actual).join(', ') + end + end + end RSpec::Matchers.define :have_queued do |*expected_args| @@ -54,11 +70,11 @@ def queue(actual) end failure_message_for_should do |actual| - "expected that #{actual} would have [#{expected_args.join(', ')}] queued#{@times_info}" + "expected that #{actual} would have [#{expected_args.join(', ')}] queued#{@times_info} but actually #{actual} with [#{actual_args_str(actual)}]" end failure_message_for_should_not do |actual| - "expected that #{actual} would not have [#{expected_args.join(', ')}] queued#{@times_info}" + "expected that #{actual} would not have [#{expected_args.join(', ')}] queued#{@times_info} but actually #{actual} with [#{actual_args_str(actual)}]" end description do @@ -128,6 +144,22 @@ def schedule_queue_for(actual) end end + def actual_args(actual) + if schedule_queue_for(actual).first + schedule_queue_for(actual).first[:args] + else + [] + end + end + + def actual_args_str(actual) + if actual_args(actual).empty? + 'no args' + else + actual_args(actual).join(', ') + end + end + end RSpec::Matchers.define :have_scheduled do |*expected_args| @@ -163,11 +195,11 @@ def schedule_queue_for(actual) end failure_message_for_should do |actual| - ["expected that #{actual} would have [#{expected_args.join(', ')}] scheduled", @time_info].join(' ') + ["expected that #{actual} would have [#{expected_args.join(', ')}] scheduled but actually #{actuall} with [#{actual_args_str(actual)}]", @time_info].join(' ') end failure_message_for_should_not 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 but actually #{actuall} with [#{actual_args_str(actual)}]", @time_info].join(' ') end description do @@ -186,11 +218,11 @@ def schedule_queue_for(actual) end failure_message_for_should do |actual| - "expected that #{actual} would have [#{expected_args.join(', ')}] scheduled" + "expected that #{actual} would have [#{expected_args.join(', ')}] scheduled but actually #{actual} with [#{actual_args_str(actual)}]" end failure_message_for_should_not do |actual| - "expected that #{actual} would not have [#{expected_args.join(', ')}] scheduled" + "expected that #{actual} would not have [#{expected_args.join(', ')}] scheduled but actually #{actual} with [#{actual_args_str(actual)}]" end description do From c8510ff0ebfd7708a3418116ba74307a12d445f1 Mon Sep 17 00:00:00 2001 From: Kensuke Nagae Date: Mon, 11 Nov 2013 14:08:27 +0900 Subject: [PATCH 2/3] Fix `actuall` to `actual` (miss spelled word) --- lib/resque_spec/matchers.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/resque_spec/matchers.rb b/lib/resque_spec/matchers.rb index 580d5d3..5f6932f 100644 --- a/lib/resque_spec/matchers.rb +++ b/lib/resque_spec/matchers.rb @@ -195,11 +195,11 @@ def actual_args_str(actual) end failure_message_for_should do |actual| - ["expected that #{actual} would have [#{expected_args.join(', ')}] scheduled but actually #{actuall} with [#{actual_args_str(actual)}]", @time_info].join(' ') + ["expected that #{actual} would have [#{expected_args.join(', ')}] scheduled but actually #{actual} with [#{actual_args_str(actual)}]", @time_info].join(' ') end failure_message_for_should_not do |actual| - ["expected that #{actual} would not have [#{expected_args.join(', ')}] scheduled but actually #{actuall} with [#{actual_args_str(actual)}]", @time_info].join(' ') + ["expected that #{actual} would not have [#{expected_args.join(', ')}] scheduled but actually #{actual} with [#{actual_args_str(actual)}]", @time_info].join(' ') end description do From 4d4885049ade00b1edfa3c38c2c6646fe6d1b006 Mon Sep 17 00:00:00 2001 From: Kensuke Nagae Date: Mon, 11 Nov 2013 16:50:05 +0900 Subject: [PATCH 3/3] Add tests for matchers' failure message added at 14243dbaf6149d68039035c449ff84d7213fa681 --- spec/resque_spec/matchers_spec.rb | 129 ++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) diff --git a/spec/resque_spec/matchers_spec.rb b/spec/resque_spec/matchers_spec.rb index b207380..f468602 100644 --- a/spec/resque_spec/matchers_spec.rb +++ b/spec/resque_spec/matchers_spec.rb @@ -50,6 +50,49 @@ end end + describe 'failure messages' do + before { Resque.enqueue(Person, first_name, last_name) } + subject { Person } + + context "when `should have_queued` failed" do + it "fails with actual arguments in failure message" do + expect { + should have_queued(last_name, first_name) + }.to raise_error(RSpec::Expectations::ExpectationNotMetError, /queued but actually #{subject} with \[#{first_name}, #{last_name}\]/) + end + + context "with no args" do + it "fails with [no args] in failure message" do + ResqueSpec.reset! + Resque.enqueue(Person) + + expect { + should have_queued(last_name, first_name) + }.to raise_error(RSpec::Expectations::ExpectationNotMetError, /queued but actually #{subject} with \[no args\]/) + end + end + end + + context "when `should_not have_queued` failed" do + it "fails with actual arguments in failure message" do + expect { + should_not have_queued(first_name, last_name) + }.to raise_error(RSpec::Expectations::ExpectationNotMetError, /queued but actually #{subject} with \[#{first_name}, #{last_name}\]/) + end + + context "with no args" do + it "fails with [no args] in failure message" do + ResqueSpec.reset! + Resque.enqueue(Person) + + expect { + should_not have_queued + }.to raise_error(RSpec::Expectations::ExpectationNotMetError, /queued but actually #{subject} with \[no args\]/) + end + end + end + end + context "#in" do before do @@ -237,6 +280,49 @@ it "returns false if the arguments are not found in the queue" do Person.should_not have_scheduled_at(scheduled_at, last_name, first_name) end + + describe 'failure messages' do + before { Resque.enqueue_at(scheduled_at, Person, first_name, last_name) } + subject { Person } + + context "when `should have_scheduled_at` failed" do + it "fails with actual arguments in failure message" do + expect { + should have_scheduled_at(scheduled_at, last_name, first_name) + }.to raise_error(RSpec::Expectations::ExpectationNotMetError, /scheduled but actually #{subject} with \[#{first_name}, #{last_name}\]/) + end + + context "with no args" do + it "fails with actual arguments in failure message" do + ResqueSpec.reset! + Resque.enqueue_at(scheduled_at, Person) + + expect { + should have_scheduled_at(scheduled_at, last_name, first_name) + }.to raise_error(RSpec::Expectations::ExpectationNotMetError, /scheduled but actually #{subject} with \[no args\]/) + end + end + end + + context "when `should_not have_scheduled_at` failed" do + it "fails with actual arguments in failure message" do + expect { + should_not have_scheduled_at(scheduled_at, first_name, last_name) + }.to raise_error(RSpec::Expectations::ExpectationNotMetError, /scheduled but actually #{subject} with \[#{first_name}, #{last_name}\]/) + end + + context "with no args" do + it "fails with actual arguments in failure message" do + ResqueSpec.reset! + Resque.enqueue_at(scheduled_at, Person) + + expect { + should_not have_scheduled_at(scheduled_at) + }.to raise_error(RSpec::Expectations::ExpectationNotMetError, /scheduled but actually #{subject} with \[no args\]/) + end + end + end + end end describe "#have_scheduled" do @@ -297,6 +383,49 @@ NoQueueClass.should have_scheduled(1).in(interval).queue(:test_queue) end end + + describe 'failure messages' do + before { Resque.enqueue_at(scheduled_at, Person, first_name, last_name) } + subject { Person } + + context "when `should have_scheduled` failed" do + it "fails with actual arguments in failure message" do + expect { + should have_scheduled(last_name, first_name) + }.to raise_error(RSpec::Expectations::ExpectationNotMetError, /scheduled but actually #{subject} with \[#{first_name}, #{last_name}\]/) + end + + context "with no args" do + it "fails with actual arguments in failure message" do + ResqueSpec.reset! + Resque.enqueue_at(scheduled_at, Person) + + expect { + should have_scheduled(last_name, first_name) + }.to raise_error(RSpec::Expectations::ExpectationNotMetError, /scheduled but actually #{subject} with \[no args\]/) + end + end + end + + context "when `should_not have_scheduled` failed" do + it "fails with actual arguments in failure message" do + expect { + should_not have_scheduled(first_name, last_name) + }.to raise_error(RSpec::Expectations::ExpectationNotMetError, /scheduled but actually #{subject} with \[#{first_name}, #{last_name}\]/) + end + + context "with no args" do + it "fails with actual arguments in failure message" do + ResqueSpec.reset! + Resque.enqueue_at(scheduled_at, Person) + + expect { + should_not have_scheduled + }.to raise_error(RSpec::Expectations::ExpectationNotMetError, /scheduled but actually #{subject} with \[no args\]/) + end + end + end + end end describe "#have_schedule_size_of" do