在 Rspec 中禁用事务装置没有效果

发布于 2024-10-10 06:21:33 字数 300 浏览 0 评论 0原文

由于我使用的是旧数据库,我只能使用 MyISAM 的 MySQL,这意味着我的表不支持事务。这导致测试失败,因为测试生成的表数据(我使用factory_girl作为固定装置)不会针对每个场景进行恢复。

我发现 Rspec 公开了spec_helper.rb 中的 config.use_transactional_fixtures 配置设置。

默认情况下设置为 true。当我将其设置为 false 时,我没有看到对我的测试有任何影响;由于重复记录,它们仍然失败。

该设置是否应该自动展开对数据库所做的任何更改?或者我应该手动执行此操作?

Due to a legacy database I'm using, I'm stuck with MySQL using MyISAM, which means my tables don't support transactions. This is causing the tests to fail, since table data generated (I'm using factory_girl for fixtures) by the tests are not reverted for each scenario.

I discovered that Rspec exposes the config.use_transactional_fixtures config setting in spec_helper.rb.

which is set to true by default. When I set it to false, I don't see any effect on my tests; they still fail due to duplicate records.

Isn't that setting supposed to automatically unroll any changes made to the DB? Or am I supposed to do that manually?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幸福%小乖 2024-10-17 06:21:33

你是对的 - 如果你的数据库不支持事务,你必须在每个语句之前发出几个 SQL 命令来擦除数据:

TRUNCATE TABLE tablename;

每个表一个。

在你的 helper.rb 中试试这个:

Spec::Runner.configure do |config|
  config.before(:each) do
    tables = %{users posts tags}
    tables.each do |t|
      ActiveRecord::Base.connection.execute('TRUNCATE TABLE #{t}')
    end
  end
  ... 
end 

You're correct - if your database doesn't support transactions you must issue several SQL commands to wipe out data before each statement:

TRUNCATE TABLE tablename;

One for each of your tables.

In your helper.rb try this:

Spec::Runner.configure do |config|
  config.before(:each) do
    tables = %{users posts tags}
    tables.each do |t|
      ActiveRecord::Base.connection.execute('TRUNCATE TABLE #{t}')
    end
  end
  ... 
end 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文