将 Ruby 2.7 + Rails 5.2 升级到 Ruby 3.x + Rails 7 会导致使用 ActiveRecord 事务的代码出现问题

问题描述 投票:0回答:0

我有以下代码(正式):

服务:

class Service
  def self.method(params)
    raise Error if some_object_exists?

    ActiveRecord::Base.transaction do
      # blah-blah create object
    end
  end
end

Rspec测试:

it "does not create the same object twice" do
  params = {}
  params2 = {}
  Service.method(params)
  Service.method(params2)

  expect { Service.method(params) }.to raise_error(Error)
end

按照标题中提到的升级 Ruby 和 Rails 后,错误没有出现。

如果我删除交易,一切都会按预期进行:存储对象并引发错误。 在此升级过程中我缺少什么?我尝试了不同的 Rspec 和应用程序配置,但没有任何改变这种行为。

activerecord rspec upgrade ruby-on-rails-7 ruby-3
© www.soinside.com 2019 - 2024. All rights reserved.