我是新来的rails。在我的项目中,我使用rspec + capybara + poltergeist + selenium + database_cleaner。测试之间的轨道清理数据库不起作用
的源代码github link
我的PostgreSQL 9.5.5,Ubuntu的LTS 16.04。
当运行测试
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
你为什么认为必须是1?根据你的设置database_cle aner可能数据库为空但不能重置id列计数器 - 您是否查询过数据库以查看其中保存了多少个问题?你的测试不应该有任何硬编码的记录ID。如果这不是问题,那么将你的一个测试和你的database_cleaner配置添加到你的问题中。 –
我将文件添加到问题。并且默认情况下,我认为,db中的这个问题是一个,并且添加文件的链接必须具有链接href:'/uploads/attachment/file/1/test_file.dat',其中1是问题标识。但实际上,问题ID是28.我认为每次测试运行时数据库清理表都会清理干净。 –
@ThomasWalpole - 我明白你在说什么。是的,桌子只有一排。我需要重写我的测试。感谢您的解决方案。 –