Created
March 19, 2012 22:17
-
-
Save dougcole/2127622 to your computer and use it in GitHub Desktop.
Rspec should_not access_database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AccessDatabase | |
def initialize | |
@query_count = 0 | |
@query_sql = [] | |
end | |
def matches?(target) | |
subscription = ActiveSupport::Notifications.subscribe(/^sql\./) do |*args| | |
@query_count += 1 | |
@query_sql << args.last[:sql] | |
end | |
target.call | |
@query_count > 0 | |
end | |
def failure_message_for_should | |
"expected block to access database" | |
end | |
def failure_message_for_should_not | |
"expected block not to access database, the following queries were run:\n#{@query_sql.join("\n")}" | |
end | |
end | |
def access_database | |
AccessDatabase.new | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Stolen from https://gist.github.com/1305807 and rewritten for rspec