gpt4 book ai didi

ruby-on-rails - Rails 4 + Devise : How to write a test for Devise Reset Password without RSpec?

转载 作者:行者123 更新时间:2023-12-04 04:51:34 28 4
gpt4 key购买 nike

由于无法控制的原因,我无法在当前项目中使用RSpec进行测试。我正在尝试测试“设计重置密码”,但似乎无法提出有用的建议。

这是我到目前为止的内容:

require 'test_helper'

class ResetPasswordTest < ActionDispatch::IntegrationTest
setup do
@user = users(:example)
end

test "reset user's password" do
old_password = @user.encrypted_password

puts @user.inspect

# This assertion works
assert_difference('ActionMailer::Base.deliveries.count', 1) do
post user_password_path, user: {email: @user.email}
end

# puts @user.reset_password_token => nil
# Not sure why this doesn't assign a reset password token to @user

patch "/users/password", user: {
reset_password_token: @user.reset_password_token,
password: "new-password",
password_confirmation: "new-password",
}

# I get a success here, but I'm not sure why, since reset password token is nil.
assert_response :success

# This assertion doesn't work.
assert_not_equal(@user.encrypted_password, old_password)
end

end

我在上面似乎无法正常工作的地方添加了一些评论。有没有人有见识,或者有关于如何更好地进行测试的想法?

最佳答案

Devise在内部测试PasswordsController。我想用额外的功能测试PasswordsController,并去Devise's controller tests寻求帮助来为Devise的默认功能构建测试,然后将新功能的断言添加到测试用例中。

如其他答案所述,您必须获取reset_password_token。您可以像使用Devise一样获取 token ,而不必像这里的其他解决方案那样解析已传递的电子邮件:

setup do
request.env["devise.mapping"] = Devise.mappings[:user]
@user = users(:user_that_is_defined_in_fixture)
@reset_password_token = @user.send_reset_password_instructions
end

查看Devise的 PasswordControllerTest解决方案:

https://github.com/plataformatec/devise/blob/master/test/controllers/passwords_controller_test.rb

关于ruby-on-rails - Rails 4 + Devise : How to write a test for Devise Reset Password without RSpec?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26848483/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com