Lee*_*lly 5 integration-testing ruby-on-rails minitest system-testing capybara
我正在测试使用Clearance身份验证gem的密码重置流程,但是我无法弄清楚如何编写一个在运行整个测试套件时通过的测试。当我单独运行它时,该测试通过,但是当我运行整个测试套件时,该测试失败,并且失败:
Capybara::ExpectationNotMet: expected "/users/467591525/password/edit?token=8fd60112cf461061eb405632f35e08a3830f661c" to equal "/users/467591525/password/edit"
Run Code Online (Sandbox Code Playgroud)
我首先尝试将其作为系统测试来进行,这是我进行其他所有此类测试的地方。那个在里面test/system/user_password_reset_test.rb,它继承自ApplicationSystemTestCaseas class UserPasswordResetTest < ApplicationSystemTestCase。
但是当我单独运行该测试作为系统测试时,会收到相同的错误消息。
如果我将其切换到集成测试,class PasswordResetTest < ActionDispatch::IntegrationTest则在单独运行时会通过测试,但Capybara::ExpectationNotMet在运行整个测试套件时会因相同的错误而失败。
这是完整的测试:
require 'application_system_test_case'
class PasswordResetTest < ActionDispatch::IntegrationTest
def setup
clear_emails
@user = users(:lee)
end
test 'User can reset their password' do
perform_enqueued_jobs do
visit sign_in_path
assert_current_path sign_in_path
click_link 'Forgot Password'
assert_current_path new_password_path
assert_title 'Password reset | Flagship'
assert_selector 'h1', text: 'Reset your password'
fill_in('Email', with: @user.email)
click_button 'Reset password'
assert_current_path passwords_path
assert_selector 'h1', text: 'Your password reset email is on the way.'
end
sleep 10
open_email(@user.email)
current_email.click_link 'Change my password'
assert_current_path(edit_user_password_path(@user, token: @user.confirmation_token))
assert_title 'Change your password | Flagship'
assert_selector 'h1', text: 'Change your password'
fill_in('Choose password', with: 'New_Password')
click_button 'Save this password'
assert_current_path(dashboard_path)
end
end
Run Code Online (Sandbox Code Playgroud)
我已经很难解决这个问题了,无法弄清楚我在做什么错。这是我的test/application_system_test_case.rb:
需要'test_helper'
# System tests
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :selenium, using: :chrome, screen_size: [1400, 1400]
include SignInHelper
end
Run Code Online (Sandbox Code Playgroud)
而我的test/test_helper.rb:
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'
require 'minitest/reporters'
Minitest::Reporters.use!
# Helpers used in tests
module SignInHelper
def sign_in_as_user_without_account
@user_without_account = users(:user_without_account)
visit root_path(as: @user_without_account)
end
def sign_in_as_account_owner
@account_owner = users(:lee)
visit root_path(as: @account_owner)
end
end
# Model tests
class ActiveSupport::TestCase
parallelize(workers: :number_of_processors)
fixtures :all
end
class ActionDispatch::IntegrationTest
# Controllers
require 'capybara/rails'
require 'capybara/minitest'
include Capybara::DSL
include SignInHelper
# Mailers
include ActiveJob::TestHelper
include Capybara::Email::DSL
end
Run Code Online (Sandbox Code Playgroud)
我在那里配置有问题吗?
更新资料
正如托马斯(Thomas)的答案建议添加的那样,ignore_query: true固定了测试下降的线。因此我更改assert_current_path(edit_user_password_path(@user, token: @user.confirmation_token))为,assert_current_path(edit_user_password_path(@user), ignore_query: true)但是现在测试仅在下一步失败。
似乎在水豚单击电子邮件中的链接时,它不会进入带有允许用户创建新密码的令牌的正确页面。而是返回带有清除Flash消息的密码重置页面:failure_when_forbidden: Please double check the URL or try submitting the form again.。
因此,似乎水豚单击链接不会以某种方式转到带有重置令牌的正确页面。但是奇怪的是,在单独运行时通过了测试,然后在每次运行整个测试套件时都失败了。
这应该仍然是一个系统测试。之后,一旦用户点击编辑密码路线,令牌很可能会被重置,因此只能使用一次。这可能会使您的比较失败。但是,您确实不需要确认令牌是路由的一部分,因为如果令牌验证失败,您将无法进入编辑密码页面,并且其余测试都会失败。因此而不是
assert_current_path(edit_user_password_path(@user, token: @user.confirmation_token))
Run Code Online (Sandbox Code Playgroud)
你可以这样做
assert_current_path(edit_user_password_path(@user), ignore_query: true)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
70 次 |
| 最近记录: |