Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added assert_queries for the AR test suite

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3784 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 5a7722de98bcf4ad932cf2ec15faa47d1d431457 1 parent 2f9442c
@technoweenie technoweenie authored
View
8 activerecord/test/abstract_unit.rb
@@ -30,7 +30,7 @@ def assert_date_from_db(expected, actual, message = nil)
end
end
- def assert_no_queries
+ def assert_queries(num = 1)
ActiveRecord::Base.connection.class.class_eval do
self.query_count = 0
alias_method :execute, :execute_with_query_counting
@@ -40,7 +40,11 @@ def assert_no_queries
ActiveRecord::Base.connection.class.class_eval do
alias_method :execute, :execute_without_query_counting
end
- assert_equal 0, ActiveRecord::Base.connection.query_count, "1 or more queries were executed"
+ assert_equal num, ActiveRecord::Base.connection.query_count, "#{ActiveRecord::Base.connection.query_count} instead of #{num} queries were executed."
+ end
+
+ def assert_no_queries(&block)
+ assert_queries(0, &block)
end
end
View
7 activerecord/test/base_test.rb
@@ -1147,6 +1147,13 @@ def test_base_class
assert_equal LoosePerson, LooseDescendant.base_class
end
+ def test_assert_queries
+ query = lambda { ActiveRecord::Base.connection.execute 'select count(*) from developers' }
+ assert_queries(2) { 2.times { query.call } }
+ assert_queries 1, &query
+ assert_no_queries { assert true }
+ end
+
# FIXME: this test ought to run, but it needs to run sandboxed so that it
# doesn't b0rk the current test environment by undefing everything.
#
Please sign in to comment.
Something went wrong with that request. Please try again.