aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorRick Olson <technoweenie@gmail.com>2006-03-05 23:41:17 +0000
committerRick Olson <technoweenie@gmail.com>2006-03-05 23:41:17 +0000
commit5a7722de98bcf4ad932cf2ec15faa47d1d431457 (patch)
treebdf2134965198a5e8572ddfc7c21f4a43c25dd4b /activerecord/test
parent2f9442c1d4470a1edc76c06cbac3097ae687c9b0 (diff)
downloadrails-5a7722de98bcf4ad932cf2ec15faa47d1d431457.tar.gz
rails-5a7722de98bcf4ad932cf2ec15faa47d1d431457.tar.bz2
rails-5a7722de98bcf4ad932cf2ec15faa47d1d431457.zip
added assert_queries for the AR test suite
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3784 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test')
-rwxr-xr-xactiverecord/test/abstract_unit.rb8
-rwxr-xr-xactiverecord/test/base_test.rb7
2 files changed, 13 insertions, 2 deletions
diff --git a/activerecord/test/abstract_unit.rb b/activerecord/test/abstract_unit.rb
index 70487980c5..6c089f3696 100755
--- a/activerecord/test/abstract_unit.rb
+++ b/activerecord/test/abstract_unit.rb
@@ -30,7 +30,7 @@ class Test::Unit::TestCase #:nodoc:
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 @@ class Test::Unit::TestCase #:nodoc:
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
diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb
index c5834f2e5b..fe74bfa7e3 100755
--- a/activerecord/test/base_test.rb
+++ b/activerecord/test/base_test.rb
@@ -1147,6 +1147,13 @@ class BasicsTest < Test::Unit::TestCase
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.
#