跳至内容 跳至搜索
方法
A

实例公共方法

assert_no_queries(include_schema: false, &block)

断言在给定块中没有执行任何 SQL 查询。

assert_no_queries { post.comments }

如果提供了:include_schema选项,则会计算任何查询(包括与架构相关的查询)。

assert_no_queries(include_schema: true) { Post.columns }
# File activerecord/lib/active_record/testing/query_assertions.rb, line 42
def assert_no_queries(include_schema: false, &block)
  assert_queries_count(0, include_schema: include_schema, &block)
end

assert_no_queries_match(match, include_schema: false, &block)

断言在给定块中没有执行任何与模式匹配的 SQL 查询。

assert_no_queries_match(/SELECT/i) { post.comments }

如果提供了:include_schema选项,则会计算与匹配器匹配的任何查询(包括与架构相关的查询)。

assert_no_queries_match(/FROM pg_attribute/i, include_schema: true) { Post.columns }
# File activerecord/lib/active_record/testing/query_assertions.rb, line 87
def assert_no_queries_match(match, include_schema: false, &block)
  assert_queries_match(match, count: 0, include_schema: include_schema, &block)
end

assert_queries_count(count = nil, include_schema: false, &block)

断言在给定块中执行的 SQL 查询数量与预期数量匹配。

# Check for exact number of queries
assert_queries_count(1) { Post.first }

# Check for any number of queries
assert_queries_count { Post.first }

如果提供了:include_schema选项,则会计算任何查询(包括与架构相关的查询)。

assert_queries_count(1, include_schema: true) { Post.columns }
# File activerecord/lib/active_record/testing/query_assertions.rb, line 18
def assert_queries_count(count = nil, include_schema: false, &block)
  ActiveRecord::Base.lease_connection.materialize_transactions

  counter = SQLCounter.new
  ActiveSupport::Notifications.subscribed(counter, "sql.active_record") do
    result = _assert_nothing_raised_or_warn("assert_queries_count", &block)
    queries = include_schema ? counter.log_all : counter.log
    if count
      assert_equal count, queries.size, "#{queries.size} instead of #{count} queries were executed. Queries: #{queries.join("\n\n")}"
    else
      assert_operator queries.size, :>=, 1, "1 or more queries expected, but none were executed.#{queries.empty? ? '' : "\nQueries:\n#{queries.join("\n")}"}"
    end
    result
  end
end

assert_queries_match(match, count: nil, include_schema: false, &block)

断言在给定块中执行的 SQL 查询与预期模式匹配。

# Check for exact number of queries
assert_queries_match(/LIMIT \?/, count: 1) { Post.first }

# Check for any number of queries
assert_queries_match(/LIMIT \?/) { Post.first }

如果提供了:include_schema选项,则会考虑与匹配器匹配的任何查询(包括与架构相关的查询)。

assert_queries_match(/FROM pg_attribute/i, include_schema: true) { Post.columns }
# File activerecord/lib/active_record/testing/query_assertions.rb, line 59
def assert_queries_match(match, count: nil, include_schema: false, &block)
  ActiveRecord::Base.lease_connection.materialize_transactions

  counter = SQLCounter.new
  ActiveSupport::Notifications.subscribed(counter, "sql.active_record") do
    result = _assert_nothing_raised_or_warn("assert_queries_match", &block)
    queries = include_schema ? counter.log_all : counter.log
    matched_queries = queries.select { |query| match === query }

    if count
      assert_equal count, matched_queries.size, "#{matched_queries.size} instead of #{count} queries were executed.#{queries.empty? ? '' : "\nQueries:\n#{queries.join("\n")}"}"
    else
      assert_operator matched_queries.size, :>=, 1, "1 or more queries expected, but none were executed.#{queries.empty? ? '' : "\nQueries:\n#{queries.join("\n")}"}"
    end

    result
  end
end