'Count issue with Rails, rspec, transactions & change block

After upgrading from Rails 5 to 6 I noticed that tests which are using ActiveRecord::Base.connection.execute started failing within rspec change blocks.

Code below raises the error even though it shouldn't (IMO). It looks like ActiveRecord::Base.connection.execute uses different transaction than standard ActiveRecord queries.

expect {
  ActiveRecord::Base.connection.execute("INSERT INTO users (...) VALUES(...)")
  raise "Transaction issue!" if ActiveRecord::Base.connection.execute("select * from users").count != User.count
}.to change {
  User.count
}

Any idea why and how to fix/address this properly?

p.s. If I run the test in a single file (rspec spec/single_test_spect.rb) it works - test fails only if all tests in the suit are being run.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source