Skip to content

Commit

Permalink
Merge pull request #9 from brigade/case-comparison
Browse files Browse the repository at this point in the history
Support passing a range to the count: option
  • Loading branch information
lencioni committed May 28, 2015
2 parents 29acf89 + d0f3877 commit ebf1858
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 26 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## master (unreleased)

- Support passing a range to the count: option, by calling the case
equality operator on the argument.

## 0.3.1

- Add `matching` option that allows you to target certain queries.
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ describe 'MyCode' do
end
end

context 'when we expect a possible range of queries' do
it 'makes database queries' do
expect { subject.make_several_queries }.to make_database_queries(count: 3..5)
end
end

context 'when we only care about manipulative queries (INSERT, UPDATE, DELETE)' do
it 'makes a destructive database query' do
expect { subject.make_one_query }.to make_database_queries(manipulative: true)
Expand Down
2 changes: 1 addition & 1 deletion lib/db_query_matchers/make_database_queries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def pluralize(count, singular, plural = nil)
'sql.active_record',
&block)
if absolute_count = options[:count]
@counter.count == absolute_count
absolute_count === @counter.count
else
@counter.count > 0
end
Expand Down
68 changes: 43 additions & 25 deletions spec/db_query_matchers/make_database_queries_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,38 +58,56 @@
end

context 'when a `count` option is specified' do
context 'and the count matches' do
it 'matches true' do
expect { subject }.to make_database_queries(count: 1)
context 'when the count is a range' do
context 'and it matches' do
it 'matches true' do
expect { subject }.to make_database_queries(count: 1..2)
end
end
end

context 'and the count does not match' do
it 'raises an error' do
expect do
expect { subject }.to make_database_queries(count: 2)
end.to raise_error
context 'and it does not match' do
it 'raises an error' do
expect do
expect { subject }.to make_database_queries(count: 2..3)
end.to raise_error
end
end
end

it 'mentions the expected number of queries' do
expect do
expect { subject }.to make_database_queries(count: 2)
end.to raise_error(RSpec::Expectations::ExpectationNotMetError,
/expected 2 queries/)
context 'when the count is an integer' do
context 'and it matches' do
it 'matches true' do
expect { subject }.to make_database_queries(count: 1)
end
end

it 'mentions the actual number of queries' do
expect do
expect { subject }.to make_database_queries(count: 2)
end.to raise_error(RSpec::Expectations::ExpectationNotMetError,
/but 1 was made/)
end
context 'and it does not match' do
it 'raises an error' do
expect do
expect { subject }.to make_database_queries(count: 2)
end.to raise_error
end

it 'lists the queries made in the error message' do
expect do
expect { subject }.to make_database_queries(count: 2)
end.to raise_error(RSpec::Expectations::ExpectationNotMetError,
/SELECT.*FROM.*cats/)
it 'mentions the expected number of queries' do
expect do
expect { subject }.to make_database_queries(count: 2)
end.to raise_error(RSpec::Expectations::ExpectationNotMetError,
/expected 2 queries/)
end

it 'mentions the actual number of queries' do
expect do
expect { subject }.to make_database_queries(count: 2)
end.to raise_error(RSpec::Expectations::ExpectationNotMetError,
/but 1 was made/)
end

it 'lists the queries made in the error message' do
expect do
expect { subject }.to make_database_queries(count: 2)
end.to raise_error(RSpec::Expectations::ExpectationNotMetError,
/SELECT.*FROM.*cats/)
end
end
end
end
Expand Down

0 comments on commit ebf1858

Please sign in to comment.