Minitest:当夹具存在时如何用空 table 测试逻辑?
Minitest: How to test logic with empty table when fixtures exist?
我有一个 table 'Project' 的固定装置,它在 table 中创建了一些条目。当我 运行 测试需要数据在 table 中时,一切正常,但我也想 运行 一个集成测试,在没有项目时测试某些横幅的存在.横幅出现在 javascript.
因此,在我的测试中,我 运行 Project.delete_all 在访问页面之前。但是,当页面加载时,它仍然显示通过固定装置创建的项目,即使我将它们从 table 中删除也是如此。就好像测试根本没有从 table 加载。
这是我的测试:
# Given I am signed in
# And I am on the projects page
# When I have no projects
# Then I see a banner prompting me to create one
scenario 'User sees prompt to create project', js: true do
Project.delete_all
visit projects_path
page.must_have_content I18n.t('project.create_project.title')
end
我将 Minitest 与 Capybara 和 selenium 或 poltergeist javascript 驱动程序一起使用。
我认为导致此行为的最可能原因是您正在删除主线程上的项目,但 Selenium 运行 位于尚未删除项目的另一个线程上。每个线程都有自己的数据库事务,因此您在一个线程上所做的更改不会反映在另一个线程上。
我会确保你的 test_helper.rb 文件中有这样的东西:
# Capybara driver
Capybara.javascript_driver = :selenium
# Make all database transactions use the same thread
ActiveRecord::ConnectionAdapters::ConnectionPool.class_eval do
def current_connection_id
Thread.main.object_id
end
end
这将确保所有数据库更改都发生在主线程上。
我有一个 table 'Project' 的固定装置,它在 table 中创建了一些条目。当我 运行 测试需要数据在 table 中时,一切正常,但我也想 运行 一个集成测试,在没有项目时测试某些横幅的存在.横幅出现在 javascript.
因此,在我的测试中,我 运行 Project.delete_all 在访问页面之前。但是,当页面加载时,它仍然显示通过固定装置创建的项目,即使我将它们从 table 中删除也是如此。就好像测试根本没有从 table 加载。
这是我的测试:
# Given I am signed in
# And I am on the projects page
# When I have no projects
# Then I see a banner prompting me to create one
scenario 'User sees prompt to create project', js: true do
Project.delete_all
visit projects_path
page.must_have_content I18n.t('project.create_project.title')
end
我将 Minitest 与 Capybara 和 selenium 或 poltergeist javascript 驱动程序一起使用。
我认为导致此行为的最可能原因是您正在删除主线程上的项目,但 Selenium 运行 位于尚未删除项目的另一个线程上。每个线程都有自己的数据库事务,因此您在一个线程上所做的更改不会反映在另一个线程上。
我会确保你的 test_helper.rb 文件中有这样的东西:
# Capybara driver
Capybara.javascript_driver = :selenium
# Make all database transactions use the same thread
ActiveRecord::ConnectionAdapters::ConnectionPool.class_eval do
def current_connection_id
Thread.main.object_id
end
end
这将确保所有数据库更改都发生在主线程上。