1

我使用 rspec+capybara 创建了几个测试。测试代码

当我用 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

但是如果我使用防护,一些往往会失败(某些测试可能会失败,并且可能不会不时)
防护输出

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

更新 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

4

1 回答 1

1

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

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

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

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

注意:为了节省时间,在您的情况下,请从下到上阅读:

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

于 2013-03-22T19:06:19.283 回答