Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show failure message with actual arguments #79

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions lib/resque_spec/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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|
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
129 changes: 129 additions & 0 deletions spec/resque_spec/matchers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down