Minitest的assert_difference在Rails 7应用中与Hotwire的Turbo不起作用。

huangapple go评论52阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年5月25日 23:00:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76333707.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定