Skip to content

Commit

Permalink
Fix a race condition in Action Text's test suite
Browse files Browse the repository at this point in the history
`assert_queries` in Action Text's test helpers can detect queries from
a previous test if background jobs are allowed to run during its
execution.

Changing the queue_adapter to the test adapter for all tests using the
helper ensure no jobs can run during its execution.
  • Loading branch information
JoeDupuis committed May 7, 2023
1 parent 9ebe125 commit fc839e9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
28 changes: 16 additions & 12 deletions actiontext/test/test_helper.rb
Expand Up @@ -24,21 +24,25 @@
end

class ActiveSupport::TestCase
def assert_queries(expected_count, &block)
ActiveRecord::Base.connection.materialize_transactions
module QueryHelpers
include ActiveJob::TestHelper

queries = []
ActiveSupport::Notifications.subscribe("sql.active_record") do |*, payload|
queries << payload[:sql] unless %w[ SCHEMA TRANSACTION ].include?(payload[:name])
end
def assert_queries(expected_count, &block)
ActiveRecord::Base.connection.materialize_transactions

result = _assert_nothing_raised_or_warn("assert_queries", &block)
assert_equal expected_count, queries.size, "#{queries.size} instead of #{expected_count} queries were executed. #{queries.inspect}"
result
end
queries = []
ActiveSupport::Notifications.subscribe("sql.active_record") do |*, payload|
queries << payload[:sql] unless %w[ SCHEMA TRANSACTION ].include?(payload[:name])
end

def assert_no_queries(&block)
assert_queries(0, &block)
result = _assert_nothing_raised_or_warn("assert_queries", &block)
assert_equal expected_count, queries.size, "#{queries.size} instead of #{expected_count} queries were executed. #{queries.inspect}"
result
end

def assert_no_queries(&block)
assert_queries(0, &block)
end
end

private
Expand Down
2 changes: 2 additions & 0 deletions actiontext/test/unit/model_test.rb
Expand Up @@ -3,6 +3,8 @@
require "test_helper"

class ActionText::ModelTest < ActiveSupport::TestCase
include QueryHelpers

test "html conversion" do
message = Message.new(subject: "Greetings", content: "<h1>Hello world</h1>")
assert_equal %Q(<div class="trix-content">\n <h1>Hello world</h1>\n</div>\n), "#{message.content}"
Expand Down

0 comments on commit fc839e9

Please sign in to comment.