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

Improve failure messages when not expecting a list #5

Merged
merged 5 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 10 additions & 1 deletion lib/rspec/sql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module Sql; end
Expected database queries: #{expected}
Actual database queries: #{matcher.actual}

Diff: #{Expectations.differ.diff_as_object(matcher.actual, expected)}
Diff: #{diff(matcher.actual, expected)}

Full query log:

Expand Down Expand Up @@ -57,6 +57,15 @@ def matcher
@matcher
end

def diff(actual, expected)
if expected.is_a?(Numeric)
change = actual - expected
format("%+d", change)
else
Expectations.differ.diff_as_object(actual, expected)
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess differ doesn't provide a method for integers, that's sad 😞

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I searched through a lot of RSpec code and couldn't find any differ doing this.


def scribe_queries(&)
queries = []

Expand Down
16 changes: 4 additions & 12 deletions spec/lib/rspec/sql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,12 @@
end

it "prints user-friendly message expecting a number" do
message = error_message { expect { User.last }.to query_database 2 }
message = error_message { expect { User.last }.to query_database 0 }
expect(message).to eq <<~TXT
Expected database queries: 2
Expected database queries: 0
Actual database queries: 1

Diff:
@@ -1 +1 @@
-2
+1

Diff: +1

Full query log:

Expand All @@ -107,11 +103,7 @@
Expected database queries: 2
Actual database queries: 1

Diff:
@@ -1 +1 @@
-2
+1

Diff: -1

Full query log:

Expand Down
Loading