如何解决发布针对Devise确认修改的测试响应,以在确认时设置密码 确认控制器 Rspec测试
我正在使用devise,并对代码进行了较小的更改,以允许未设置密码的用户在单击确认电子邮件链接时设置密码。
我看到两个需要修复的问题:
-
我的redirect(to)的期望()。缺少reset_password_token传递给edit_user_password_path。
-
最后一个测试“应该重定向到编辑密码页面”返回200,而我认为它应该返回重定向302,就像第二个测试一样。为什么会返回200?
确认控制器
class ConfirmationsController < Devise::ConfirmationsController
skip_before_action :authenticate_user!
def show
super {
if resource.confirmed? && resource.encrypted_password.blank?
error_index = resource.errors.details[:email].find_index{|i| i[:error] == :already_confirmed}
if error_index.present?
resource.errors.messages[:email].delete_at(error_index)
resource.errors.details[:email].delete_at(error_index)
end
end
}
end
protected
def after_confirmation_path_for(resource_name,resource)
if resource.encrypted_password.blank?
token = resource.send(:set_reset_password_token)
edit_password_url(resource,reset_password_token: token)
else
super
end
end
end
Rspec测试
# frozen_string_literal: true
require "rails_helper"
RSpec.describe ConfirmationsController,type: :controller do
before do
@request.env["devise.mapping"] = Devise.mappings[:user]
setup_controller_for_warden
end
describe "GET" do
describe "#show" do
describe "User confirmed" do
describe "when signed in" do
describe "with password" do
it "should error on #new page with :already_confirmed" do
user = FactoryBot.create(:user)
login_user(user,confirm: true)
get :show,params: {confirmation_token: user.confirmation_token}
expect(response.status).to eq 200
end
end
describe "without password" do
it "should redirect to edit password page" do
@user = FactoryBot.create(:user,:no_password)
login_user(@user,params: {confirmation_token: @user.confirmation_token}
expect(response.status).to eq 302
expect(response).to redirect_to(edit_user_password_path)
end
end
end
describe "when signed out" do
describe "with password" do
it "should error on #new page with :already_confirmed" do
user = FactoryBot.create(:user)
user.confirm
get :show,params: {confirmation_token: user.confirmation_token}
expect(response.status).to eq 200
end
end
describe "without password" do
it "should redirect to edit password page" do
user = FactoryBot.create(:user,:no_password)
user.confirm
get :show,params: {confirmation_token: user.confirmation_token}
expect(response.status).to eq 302
expect(response).to redirect_to(edit_user_password_path)
end
end
end
end
describe "User Unconfirmed" do
describe "when signed in" do
describe "with password" do
it "should redirect to root path" do
user = FactoryBot.create(:user)
login_user(user,confirm: false)
get :show,params: {confirmation_token: user.confirmation_token}
expect(response.status).to eq 302
expect(response).to redirect_to(root_path)
end
end
describe "without password" do
it "should redirect to edit password page" do
user = FactoryBot.create(:user,:no_password)
login_user(user,params: {confirmation_token: user.confirmation_token}
expect(response.status).to eq 302
expect(response).to redirect_to(edit_user_password_path)
end
end
end
describe "when signed out" do
describe "with password" do
it "should redirect to sign-in (new session) path" do
user = FactoryBot.create(:user)
get :show,params: {confirmation_token: user.confirmation_token}
expect(response.status).to eq 302
expect(response).to redirect_to(new_user_session_path)
end
end
describe "without password" do
it "should redirect to edit password page" do
user = FactoryBot.create(:user)
user.confirm
get :show,params: {confirmation_token: user.confirmation_token}
expect(response.status).to eq 200
expect(response).to redirect_to(edit_user_password_path)
end
end
end
end
end
end
end
错误:
Failures:
1) ConfirmationsController GET #show User confirmed when signed in without password should redirect to edit password page
Failure/Error: expect(response).to redirect_to(edit_user_password_path)
Expected response to be a redirect to <http://test.host/users/password/edit> but was a redirect to <http://test.host/users/password/edit?reset_password_token=aMrkaAYQxCcAzMZy3UpX>.
Expected "http://test.host/users/password/edit" to be === "http://test.host/users/password/edit?reset_password_token=aMrkaAYQxCcAzMZy3UpX".
# ./spec/controllers/confirmations_controller_spec.rb:30:in `block (7 levels) in <top (required)>'
2) ConfirmationsController GET #show User confirmed when signed out without password should redirect to edit password page
Failure/Error: expect(response).to redirect_to(edit_user_password_path)
Expected response to be a redirect to <http://test.host/users/password/edit> but was a redirect to <http://test.host/users/password/edit?reset_password_token=w1tzGFDjwWsn876cm96K>.
Expected "http://test.host/users/password/edit" to be === "http://test.host/users/password/edit?reset_password_token=w1tzGFDjwWsn876cm96K".
# ./spec/controllers/confirmations_controller_spec.rb:50:in `block (7 levels) in <top (required)>'
3) ConfirmationsController GET #show User Unconfirmed when signed in without password should redirect to edit password page
Failure/Error: expect(response).to redirect_to(edit_user_password_path)
Expected response to be a redirect to <http://test.host/users/password/edit> but was a redirect to <http://test.host/users/password/edit?reset_password_token=Jzz6p2ZEzjPbbVjUeMZT>.
Expected "http://test.host/users/password/edit" to be === "http://test.host/users/password/edit?reset_password_token=Jzz6p2ZEzjPbbVjUeMZT".
# ./spec/controllers/confirmations_controller_spec.rb:74:in `block (7 levels) in <top (required)>'
4) ConfirmationsController GET #show User Unconfirmed when signed out without password should redirect to edit password page
Failure/Error: expect(response).to redirect_to(edit_user_password_path)
Expected response to be a <3XX: redirect>,but was a <200: OK>
# ./spec/controllers/confirmations_controller_spec.rb:95:in `block (7 levels) in <top (required)>'
Finished in 1.03 seconds (files took 3.45 seconds to load)
8 examples,4 failures
Failed examples:
rspec ./spec/controllers/confirmations_controller_spec.rb:25 # ConfirmationsController GET #show User confirmed when signed in without password should redirect to edit password page
rspec ./spec/controllers/confirmations_controller_spec.rb:45 # ConfirmationsController GET #show User confirmed when signed out without password should redirect to edit password page
rspec ./spec/controllers/confirmations_controller_spec.rb:69 # ConfirmationsController GET #show User Unconfirmed when signed in without password should redirect to edit password page
rspec ./spec/controllers/confirmations_controller_spec.rb:90 # ConfirmationsController GET #show User Unconfirmed when signed out without password should redirect to edit password page
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。