thoughtbot/shoulda-matchers — pull request #69 by MDaubs (2012-03-07 21:18:22+00:00)
Title: Added QueryTheDatabaseMatcher

This might be controversial but I was hoping for some feedback. I've found myself spec'ing the number of database calls made by methods that iterate over datasets as a means of preventing situations where a small code change causes O(N) queries instead of O(1) queries while not actually failing any specs. Situations like a join query not being eager-loaded due to a missing call to #include. It's been very useful to me and I haven't really seen anything like it in the community so I thought it might make a great addition to shoulda.

This matcher can only be used in Rails 3.1 or greater due to Rails 3.0's ambiguous notification naming scheme.

Example:

describe TpsReport do
it { should query_the_database(4.times).when_calling(:regenerate_report) }
it { should_not query_the_database.when_calling(:cached_report) }
end