Database_Cleaner 在规范中间销毁 HTTP 请求之间的记录

发布于 2025-01-07 03:28:22 字数 1618 浏览 3 评论 0原文

我正在使用 Rspec 和 Capybara 运行集成规范,并使用 Database Cleaner 清理规范之间的记录。如果重要的话,我会使用 Guard 和 Spork 自动运行我的规范。

不知何故,在测试运行过程中,记录被从数据库中删除,导致它们失败。我是否错误地设置了数据库清理器?或者我做错了什么?我已经看过这篇文章,但我没有我不认为这是我的问题。

任何帮助将不胜感激!

这是spec_helper.rb

Spork.prefork do
  # ...    
  RSpec.configure do |config|
    config.mock_with :rspec
    config.use_transactional_fixtures = true
    config.include(MailerMacros)

    config.treat_symbols_as_metadata_keys_with_true_values = true
    config.filter_run :focus => true
    config.run_all_when_everything_filtered = true

    config.before(:suite) { DatabaseCleaner.strategy = :truncation }
    config.before(:each) { DatabaseCleaner.start }
    config.after(:each) { DatabaseCleaner.clean }
  end

end

Spork.each_run do
  FactoryGirl.reload
end

这是我的规范:(注意 2 个 put 语句)

feature "Claim Firm" do
  let(:firm) { Factory(:firm, :user_id => nil) }

  scenario "The details page should show a 'Claim' link", :focus => true do
    email = '[email protected]'
    password = 'secretpass123'
    u = Factory(:user, :email => email, :password => password, :name => "Bob Claimer")
    puts "User Count: #{User.count}" # ==> 1
    visit login_path
    puts "User Count: #{User.count}" # ==> 0
    # The rest of the spec fails because there are no user records...
  end
end

I'm running integration specs using Rspec and Capybara and cleaning out records between specs using Database Cleaner. If it matters, I'm running my specs automatically using Guard and Spork.

Somehow, in the middle of a test run, records are being deleted from the database, causing them to fail. Have I setup Datbase Cleaner incorrectly? Or am I doing something else wrong? I saw this post already, but I don't think this is my problem.

Any help would be appreciated!

Here is spec_helper.rb

Spork.prefork do
  # ...    
  RSpec.configure do |config|
    config.mock_with :rspec
    config.use_transactional_fixtures = true
    config.include(MailerMacros)

    config.treat_symbols_as_metadata_keys_with_true_values = true
    config.filter_run :focus => true
    config.run_all_when_everything_filtered = true

    config.before(:suite) { DatabaseCleaner.strategy = :truncation }
    config.before(:each) { DatabaseCleaner.start }
    config.after(:each) { DatabaseCleaner.clean }
  end

end

Spork.each_run do
  FactoryGirl.reload
end

And here is my spec: (notice the 2 puts statements)

feature "Claim Firm" do
  let(:firm) { Factory(:firm, :user_id => nil) }

  scenario "The details page should show a 'Claim' link", :focus => true do
    email = '[email protected]'
    password = 'secretpass123'
    u = Factory(:user, :email => email, :password => password, :name => "Bob Claimer")
    puts "User Count: #{User.count}" # ==> 1
    visit login_path
    puts "User Count: #{User.count}" # ==> 0
    # The rest of the spec fails because there are no user records...
  end
end

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

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

发布评论

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

评论(1

我的奇迹 2025-01-14 03:28:22

不确定根本原因是什么,但 SQLite 的截断策略是一种优化,在某些情况下似乎表现得很有趣,所以坚持使用 :transaction 或 :deletion 如果它们不是太慢的话。

Not sure what the root cause is, but the truncation strategy for SQLite is an optimization that seems to behave funny in certain situations, so stick with :transaction or :deletion if they're not too slow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文