英文:
Minitest's assert_difference not working with Hotwire's Turbo in a Rails 7 app
问题
I'm upgrading a Rails App in order to use Hotwire but tests like the following, since I enabled Turbo, are now failing:
class UserApprovalTest < ApplicationSystemTestCase
driven_by :selenium_chrome_headless
test 'welcome email gets sent on new user approval' do
#...
assert_difference('ActionMailer::Base.deliveries.count', 1) do # <= this used to PASS before enabling Turbo
click_link("super_admin_approves_user_#{user.id}") # link with data-turbo="true"
# ...
end
end
The expected behavior is that when a super user approves a new user, the app sends a welcome email. This still works in the development environment when I test by hand, but now the system test above fails.
Why?
How can I fix it, please?
英文:
I'm upgrading a Rails App in order to use Hotwire but tests like the following, since I enabled Turbo, are now failing:
class UserApprovalTest < ApplicationSystemTestCase
driven_by :selenium_chrome_headless
test 'welcome email gets sent on new user approval' do
#...
assert_difference('ActionMailer::Base.deliveries.count', 1) do # <= this used to PASS before enabling Turbo
click_link("super_admin_approves_user_#{user.id}") # link with data-turbo="true"
# ...
end
end
The expected behavior is that when a super user approves a new user, the app sends a welcome email. This still works in the development environment when I test by hand, but now the system test above fails.
Why?
How can I fix it, please?
答案1
得分: 1
If it's not an error in your JS then it's likely because the actions are now being executed asynchronously, and your assertion is being executed before the action has occurred. You need to assert for something that indicates the action has completed to keep your test in sync (assert_difference isn't provided by Capybara and therefore doesn't have retrying behavior)
assert_difference('ActionMailer::Base.deliveries.count', 1) do
click_link("super_admin_approves_user_#{user.id}")
assert_content(...) # any Capybara assertion for something that changes on the page to indicate the action has completed
end
英文:
If it's not an error in your JS then it's likely because the actions are now being executed asynchronously, and your assertion is being executed before the action has occurred. You need to assert for something that indicates the action has completed to keep your test in sync (assert_difference isn't provided by Capybara and therefore doesn't have retrying behavior)
assert_difference('ActionMailer::Base.deliveries.count', 1) do
click_link("super_admin_approves_user_#{user.id}")
assert_content(...) # any Capybara assertion for something that changes on the page to indicate the action has completed
end
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论