gpt4 book ai didi

ruby-on-rails - 测试页面重定向的问题

转载 作者:太空宇宙 更新时间:2023-11-03 16:18:10 27 4
gpt4 key购买 nike

我一直在根据 Micheal Hartl 的教程学习 Ruby on Rails,遇到了一个非常有趣的问题,即如果我网站上的某些用户试图通过点击适当的网址。问题是,当我启动 rails 服务器并手动检查此操作时,它实际上将我重定向到根 url,但是,规范测试显示:

Failure/Error: specify { expect(response).to redirect_to(root_url) }
Expected response to be a redirect to <http://www.example.com/> but was a redirect to <http://www.example.com/signin>.
Expected "http://www.example.com/" to be === "http://www.example.com/signin".
# ./spec/requests/authentication_pages_spec.rb:95:in `block (5 levels) in <top (required)>'

我在网上搜索以找到解决这个问题的方法,发现实际上有几个类似的问题,人们建议在 rb 中使用 no_capybara: true > 文件,但是,我已经使用了这个解决方案。本次测试的代码部分如下:

describe "as wrong user" do
let(:user) { FactoryGirl.create(:user) }
let(:wrong_user) { FactoryGirl.create(:user, email: "wrong@example.com") }
before { sign_in user, no_capybara: true }

describe "submitting a GET request to the Users#edit action" do
before { get edit_user_path(wrong_user) }
specify { expect(response.body).not_to match(full_title('Edit user')) }
specify { expect(response).to redirect_to(root_url) }
end

describe "submitting a PATCH request to the Users#update action" do
before { patch user_path(wrong_user) }
specify { expect(response).to redirect_to(root_url) }
end
end

Controller 代码也在这里:

class UsersController < ApplicationController
before_action :signed_in_user, only: [:index, :edit, :update]
before_action :correct_user, only: [:edit, :update]

def index
@users = User.all
end

def show
@user = User.find(params[:id])
end

def new
@user = User.new
end

def create
@user=User.new(user_params)
if @user.save
sign_in @user
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
render 'new'
end
end

def edit
end

def update
if @user.update_attributes(user_params)
flash[:success] = "Profile updated"
redirect_to @user
else
render 'edit'
end
end

private

def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end

#Before filters

def signed_in_user
unless signed_in?
store_location
redirect_to signin_url, notice: "Please sign in."
end
end

def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user)
end
end

这里还有 Controller 的 session 助手代码:

def current_user?(user)
user == current_user
end

def sign_out
current_user.update_attribute(:remember_token, User.encrypt(User.new_remember_token))
cookies.delete(:remember_token)
self.current_user = nil
end

def redirect_back_or(default)
redirect_to(session[:return_to] || default)
session.delete(:return_to)
end

我会很高兴得到任何答案,并准备好提供任何细节!

最佳答案

用户的sign_in 似乎不起作用。请首先检查用户是否真的通过调试器/byebug 登录,或者只是编写一个小测试用例以使用 get edit_user_path(user) 进行验证(它不应重定向)。

您可能还想检查您的测试 sign_in 方法是否正确实现:Can't understand no_capybara option in michael hart'l tutorial test .这也是为什么必须在此处使用 no_capybara 选项的解释。

关于ruby-on-rails - 测试页面重定向的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39335998/

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