我是铁杆新手。在我的项目中,我使用rspec + capybara + poltergeist + selenium + database_cleaner。
源代码github link
我有postgresql 9.5.5,ubuntu 16.04 LTS。
运行测试时
rspec spec/controllers # everything work fine
运行时
rspec spec/features # everything work fine too
但是当我运行所有测试时
rspec # part of tests fail
当运行第一次验收测试时:硒 - >浏览器28中的question_id,但必须为1,因为它使用database_cleaner。
为什么database_cleaner没有清理我的数据库?我做错了什么?我花了一天时间找到解决方案,但一无所获。 请帮帮我。
P.S。这是一个培训项目。
我的Database_Cleaner配置是:
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, js: true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
Warden.test_reset!
if Rails.env.test?
FileUtils.rm_rf(Dir["#{Rails.root}/public/uploads"])
end
end
答案 0 :(得分:3)
清理表意味着删除所有记录,它并不一定意味着它会重置索引计数器 - 就像删除记录3然后添加新记录一样 - 新记录将是4.所以在你的虽然只有1条实际记录,但它可能删除了27条记录,而你创建的下一条记录将是28条记录。查询数据库以查看有多少实际记录来验证。
接下来,您的数据库清理器配置应该更像推荐的配置 - https://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example。 append_after vs after对于稳定性和检查驱动程序名称而言非常重要,只需:js元数据也是如此。
检查href' /uploads/attachment/file/1/test_file.dat'在你的测试中,你不应该检查硬编码的' 1'你应该根据你创建的记录的记录ID号进行检查。所以" / uploads / attachment / file /#{question.id} /test_file.dat" (显然question.id
取决于您创建的对象和变量名称,但这至少与我认为的一个测试相匹配。
此外,在快速查看您的规格后 - 您所做的任何地方expect(current_path).to eq ...
都是错误的。你应该做expect(page).to have_current_path(...)
。第一种方法禁用Capybara的重试行为,并将导致片状测试。
最后 - 你有expect(page).not_to have_content t('common.button.ready')
,我猜这应该是expect(page).not_to have_button t('common.button.ready')
答案 1 :(得分:0)
在这里,您可以了解如何使用database_cleaner
和RSpec
- https://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example配置Capybara