计算 Rails、rspec、交易和事务的问题改变块
从 Rails 5 升级到 6 后,我注意到使用 ActiveRecord::Base.connection.execute 的测试开始在 rspec 更改块内失败。
下面的代码会引发错误,尽管它不应该(IMO)。看起来 ActiveRecord::Base.connection.execute 使用与标准 ActiveRecord 查询不同的事务。
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
}
知道为什么以及如何正确修复/解决这个问题吗?
ps 如果我在单个文件 (rspec spec/single_test_spect.rb
) 中运行测试,它会起作用 - 仅当运行套件中的所有测试时测试才会失败。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论