【问题标题】:Rspec with capybara strange behavior when guard is running警卫运行时水豚奇怪行为的Rspec
【发布时间】:2013-03-11 10:06:29
【问题描述】:

我使用 rspec+capybara 创建了几个测试。 tests code

当我用 rspec 运行它们时,它们都通过了

git:(master) ✗ rspec    
Rack::File headers parameter replaces cache_control after Rack 1.5.
WARNING:  there is already a transaction in progress
NOTICE:  there is no transaction in progress
.WARNING:  there is already a transaction in progress
NOTICE:  there is no transaction in progress
.WARNING:  there is already a transaction in progress
NOTICE:  there is no transaction in progress
.WARNING:  there is already a transaction in progress
NOTICE:  there is no transaction in progress
.WARNING:  there is already a transaction in progress
NOTICE:  there is no transaction in progress
.WARNING:  there is already a transaction in progress
NOTICE:  there is no transaction in progress
.WARNING:  there is already a transaction in progress
NOTICE:  there is no transaction in progress
.WARNING:  there is already a transaction in progress
NOTICE:  there is no transaction in progress
.

Finished in 32.54 seconds
8 examples, 0 failures

但是如果我使用 guard 一些往往会失败(有些测试可能会失败,并且可能不会时常失败)
Guard output

如何解释这种行为?以及如何解决?

更新 1
我已经在使用gem 'database_cleaner' 这个配置:

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

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

  config.after(:each) do
    DatabaseCleaner.clean
  end
  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = :transaction
  config.use_transactional_examples = true

更新 2
更改了几个文件https://github.com/Asmmund/notes/commit/a5e0a43d6247bb8f937fb7e9dcc8d8cfa7bfc4ea

【问题讨论】:

  • 也许有些记录会保留在测试数据库中,而它们不应该保留?
  • @MikhailNikalyukin 请参阅更新 1

标签: ruby-on-rails rspec capybara guard


【解决方案1】:

根据您编写测试的方式,Capybara 使用 Selenium 驱动程序来运行它们。 Selenium 与事务性装置不兼容,您应该将其设置为 false。

一般来说,您应该尽量避免将 Fixtures 与 Capybara(或大多数集成测试)一起使用。一个好的集成测试应该最低限度地依赖于夹具,而是在端到端流程中创建数据(例如,用户帐户应该通过模拟应用内操作创建,即测试应该使用 Capybara 提交注册表单)。但奇怪的情况是无法避免的,您可能需要在数据库中植入一些数据。许多人为此使用 FactoryGirl 等工厂,并取得了成功。

如果您的测试是 selenium,那么您的数据库清理也需要使用截断策略,而不是事务策略。

查看本教程作为更深入的指南。

注意:为了节省时间,请从下往上阅读:

http://asciicasts.com/episodes/257-request-specs-and-capybara

【讨论】:

  • 谢谢,我今天就这样做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多