Skip to content

Instantly share code, notes, and snippets.

@dougcole
Created March 19, 2012 22:17
Show Gist options
  • Save dougcole/2127622 to your computer and use it in GitHub Desktop.
Save dougcole/2127622 to your computer and use it in GitHub Desktop.
Rspec should_not access_database
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
@dougcole
Copy link
Author

Stolen from https://gist.github.com/1305807 and rewritten for rspec

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment