diff --git a/lib/resque_spec/matchers.rb b/lib/resque_spec/matchers.rb index 0612fc1..5f6932f 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 #{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", @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 @@ -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 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