1
0
Fork 0
mirror of https://github.com/thoughtbot/shoulda-matchers.git synced 2022-11-09 12:01:38 -05:00

Continue using options hash.

This commit is contained in:
Gabe Berke-Williams 2012-04-23 18:04:54 -04:00
parent a718c46814
commit 2b9591052f

View file

@ -20,65 +20,66 @@ module Shoulda # :nodoc:
class QueryTheDatabaseMatcher # :nodoc: class QueryTheDatabaseMatcher # :nodoc:
def initialize(times) def initialize(times)
@queries = []
@options = {}
if times.respond_to?(:count) if times.respond_to?(:count)
@expected_query_count = times.count @options[:expected_query_count] = times.count
else else
@expected_query_count = times @options[:expected_query_count] = times
end end
end end
def when_calling(method_name) def when_calling(method_name)
@method_name = method_name @options[:method_name] = method_name
self self
end end
def with(*method_arguments) def with(*method_arguments)
@method_arguments = method_arguments @options[:method_arguments] = method_arguments
self self
end end
def or_less def or_less
@expected_count_is_maximum = true @options[:expected_count_is_maximum] = true
self self
end end
def matches?(subject) def matches?(subject)
@queries = []
subscriber = ActiveSupport::Notifications.subscribe('sql.active_record') do |name, started, finished, id, payload| subscriber = ActiveSupport::Notifications.subscribe('sql.active_record') do |name, started, finished, id, payload|
@queries << payload unless filter_query(payload) @queries << payload unless filter_query(payload)
end end
if @method_arguments if @options[:method_arguments]
subject.send(@method_name, *@method_arguments) subject.send(@options[:method_name], *@options[:method_arguments])
else else
subject.send(@method_name) subject.send(@options[:method_name])
end end
ActiveSupport::Notifications.unsubscribe(subscriber) ActiveSupport::Notifications.unsubscribe(subscriber)
if @expected_count_is_maximum if @options[:expected_count_is_maximum]
@queries.length <= @expected_query_count @queries.length <= @options[:expected_query_count]
elsif @expected_query_count.present? elsif @options[:expected_query_count].present?
@queries.length == @expected_query_count @queries.length == @options[:expected_query_count]
else else
@queries.length > 0 @queries.length > 0
end end
end end
def failure_message def failure_message
if @expected_query_count if @options.key?(:expected_query_count)
"Expected ##{@method_name} to cause #{@expected_query_count} database queries but it actually caused #{@queries.length} queries:" + friendly_queries "Expected ##{@options[:method_name]} to cause #{@options[:expected_query_count]} database queries but it actually caused #{@queries.length} queries:" + friendly_queries
else else
"Expected ##{@method_name} to query the database but it actually caused #{@queries.length} queries:" + friendly_queries "Expected ##{@options[:method_name]} to query the database but it actually caused #{@queries.length} queries:" + friendly_queries
end end
end end
def negative_failure_message def negative_failure_message
if @expected_query_count if @options[:expected_query_count]
"Expected ##{@method_name} to not cause #{@expected_query_count} database queries but it actually caused #{@queries.length} queries:" + friendly_queries "Expected ##{@options[:method_name]} to not cause #{@options[:expected_query_count]} database queries but it actually caused #{@queries.length} queries:" + friendly_queries
else else
"Expected ##{@method_name} to not query the database but it actually caused #{@queries.length} queries:" + friendly_queries "Expected ##{@options[:method_name]} to not query the database but it actually caused #{@queries.length} queries:" + friendly_queries
end end
end end