Skip to content

Commit

Permalink
Add syntactic sugar for x.times
Browse files Browse the repository at this point in the history
  • Loading branch information
mkllnk committed Feb 29, 2024
1 parent 0f94ee5 commit f98862d
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ expect { User.last }.to query_database
# Assert no database queries:
expect { nil }.to_not query_database

# Assert exactly one query:
# Assert exact number of queries:
expect { User.last }.to query_database 1
expect { User.create }.to query_database 3.times

# Assert specific queries:
expect { User.last }.to query_database ["User Load"]
Expand Down
2 changes: 2 additions & 0 deletions lib/rspec/sql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ module Sql; end
!@queries.empty?
elsif expected.is_a?(Integer)
@queries.size == expected
elsif expected.is_a?(Enumerator) && expected.inspect.match?(/:times>$/)
@queries.size == expected.size
elsif expected.is_a?(Array)
query_names == expected
else
Expand Down
1 change: 1 addition & 0 deletions spec/lib/rspec/sql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

it "expects a number of database queries" do
expect { User.last }.to query_database 1
expect { User.create! }.to query_database 3.times

expect {
expect { User.last }.to query_database 2
Expand Down

0 comments on commit f98862d

Please sign in to comment.