我使用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 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
答案 0 :(得分:1)
根据您编写测试的方式,Capybara使用Selenium驱动程序来运行它们。 Selenium与事务夹具不兼容,您应该将其设置为false。
通常,您应该尽量避免在Capybara(或大多数集成测试)中使用Fixtures。良好的集成测试应该最少依赖于灯具,而是在端到端流程中创建数据(例如,用户帐户应该通过模拟的应用内操作创建,即测试应该使用Capybara提交注册表单)。但奇怪的情况是不可避免的,你可能需要在数据库中添加一些数据。很多人为了这个而使用FactoryGirl这样的工厂,并取得了成功。
如果您的测试是selenium,那么您的数据库清理还需要使用截断,而不是事务策略。
查看本教程作为更深入的指南。
注意:为节省时间,在您的情况下,请从下到上阅读:
http://asciicasts.com/episodes/257-request-specs-and-capybara