gpt4 book ai didi

ruby-on-rails - Ruby on Rails 教程 : Chapter 9. 2 测试失败

转载 作者:行者123 更新时间:2023-12-04 05:31:45 24 4
gpt4 key购买 nike

我正在学习 Michael Hartl 教程的最新版本,但无法通过第 9.2 章中的几个测试:

http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-users#sec-authorization

我已经验证了我的 gem 版本,重新启动了 Rails 服务器,运行了包更新,并重建了测试数据库,但无济于事。我已经从 git 存储库中重新复制了,并检查了我认为相关的每一行。我在第 9 章中没有遇到任何问题,但我正在努力做到彻底,尤其是像这样的网络安全部分,因为我想在完成本教程后使用此模型创建一个新站点。任何帮助深表感谢。

作为旁注,编辑重定向似乎工作正常,但使用 PUT 的测试失败了,即使它们在 Controller 中使用相同的重定向功能,我不明白为什么它们的行为会有所不同。再次感谢您的帮助。

约翰

失败信息:

1) Authentication authorization for non-signed-in users in the Users controller submitting to the update action Failure/Error: specify { response.should redirect_to(signin_path) } Expected response to be a redirect to >http://www.example.com/signin but was a redirect to >https://www.example.com/users/45 # ./spec/requests/authentication_pages_spec.rb:60:in `block (6 levels) in top (required)'

2) Authentication authorization as wrong user submitting a PUT request to the Users#update action Failure/Error: specify { response.should redirect_to(root_path) } Expected response to be a redirect to >http://www.example.com/ but was a redirect to >https://www.example.com/users/49 # ./spec/requests/authentication_pages_spec.rb:77:in `block (5 levels) in 'top (required)'



这是身份验证规范,其中 2 个失败的测试来自:
需要'spec_helper'
describe "Authentication" do

subject { page }

describe "signin page" do [...]

describe "signin" do [...]

describe "authorization" do

describe "for non-signed-in users" do
let(:user) { FactoryGirl.create(:user) }

describe "in the Users controller" do

describe "visiting the edit page" do
before { visit edit_user_path(user) }
it { should have_selector('title', text: 'Sign in') }
end

describe "submitting to the update action" do
before { put user_path(user) }
specify { response.should redirect_to(signin_path) } #<---Failure 1
end
end
end

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

describe "visiting Users#edit page" do
before { visit edit_user_path(wrong_user) }
it { should_not have_selector('title', text: full_title('Edit user')) }
end

describe "submitting a PUT request to the Users#update action" do
before { put user_path(wrong_user) }
specify { response.should redirect_to(root_path) } #<--- Failure 2
end
end
end
end

实用程序中的 sign_in 函数:
def sign_in(user)
visit signin_path
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
# Sign in when not using Capybara as well.
cookies[:remember_token] = user.remember_token
end

这是用户 Controller :
class UsersController < ApplicationController
before_filter :signed_in_user, only: [:edit, :update]
before_filter :correct_user, only: [:edit, :update]

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

def new
@user = User.new
end

def create
@user = User.new(params[:user])
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(params[:user])
flash[:success] = "Profile updated"
sign_in @user
redirect_to @user
else
render 'edit'
end
end

private

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

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

还有我的路线,以防万一:
Railstut::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]

root to: 'static_pages#home'

match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete

match '/help', to: "static_pages#help"
match '/about', to: "static_pages#about"
match '/contact', to: "static_pages#contact"
end

最佳答案

我想通了。这是因为我在应用程序 Controller 中打开了 SSL。我添加了一个环境测试,现在一切都通过了。

class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper

if Rails.env.production?
force_ssl
end
end

关于ruby-on-rails - Ruby on Rails 教程 : Chapter 9. 2 测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12488000/

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