rspec - Rails 中的 TX 不应该有 requires_new : true 的任何原因

标签 rspec ruby-on-rails-4 database-cleaner

与此问题相关:custom transaction doesn't work with database_cleaner in rspec

有什么地方是人们不想用 requires_new 启动 TX 的吗? (嵌套 TX)

ModelObject.transaction(requires_new: true)

如果是这样的话,这应该是默认的吧。

顺便说一句,这就是使用默认事务策略时,当代码的正常执行路径成功时,带有回滚的 rspec 测试将失败的原因。

这是我的 DatabaseCleaner 设置。运行 Rails 4.

RSpec.configure do |config|
  config.add_setting(:seed_tables)
  config.seed_tables = %w(global_options shoot_types)

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation, except: config.seed_tables)
  end

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

  config.before(:each, js: true) do
    DatabaseCleaner.strategy = :truncation, {except: config.seed_tables}
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end

end

最佳答案

In order to get a ROLLBACK for the nested transaction you may ask for a real sub-transaction by passing requires_new: true. If anything goes wrong, the database rolls back to the beginning of the sub-transaction without rolling back the parent transaction. If we add it to the previous example:

Most databases don’t support true nested transactions. At the time of writing, the only database that we’re aware of that supports true nested transactions, is MS-SQL. Because of this, Active Record emulates nested transactions by using savepoints on MySQL and PostgreSQL. See dev.mysql.com/doc/refman/5.6/en/savepoint.html for more information about savepoints.

http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html

关于rspec - Rails 中的 TX 不应该有 requires_new : true 的任何原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20409989/

相关文章:

javascript - 测试javascript点击功能capybara附加文件图片上传

ruby-on-rails-3 - 数据库清理器不清理一个 cucumber 场景

html - 使用 capybara 点击基于 ID 的链接

ruby-on-rails - 使用 rspec 测试导出到 excel 或 csv 的最佳方法是什么?

ruby-on-rails - 生成漂亮的:scaffold=> create_migration': wrong number of arguments (3 for 0) (ArgumentError`)时出错

ruby-on-rails - Rails 4 强参数 : can I 'exclude' /blacklist attributes instead of permit/whitelist?

ruby-on-rails - 在前端运行集成/验收测试。前端需要一个 API 来告诉 Rails 为每个测试设置哪个数据库状态

ruby-on-rails - 正在运行的规范条目正在从模式迁移表中删除

rspec - rspec 中特定上下文的一次性配置?

ruby - 为什么使用 AJAX 的 rspec 示例无法正常工作?