Ruby on rails RubyonRails教程:第9.2章测试失败
我正在学习Michael Hartl教程的最新版本,但我无法通过第9.2章中的几个测试: 我已经验证了我的gem版本,重新启动了Rails服务器,运行了bundle更新,并重建了测试数据库,但都没有用。我已经从git存储库中重新编译了,并且浏览了我认为相关的每一行。我在第9章中没有遇到任何问题,但我正在努力深入,尤其是像这一章这样的web安全部分,因为我想在完成教程后使用此模型创建一个新网站。非常感谢您的帮助 作为旁注,编辑重定向似乎工作正常,但是使用PUT的测试失败了,即使它们在控制器中使用相同的重定向功能,我不理解为什么它们会有不同的行为。再次感谢您的帮助 约翰 失败消息: 1) 提交更新操作的用户控制器中未登录用户的身份验证授权 失败/错误:指定{response.should_重定向到(signin_path)} 预期响应是重定向到>http://www.example.com/signin 但是是重定向到>https://www.example.com/users/45 #./spec/requests/authentication\u pages\u spec.rb:60:in`block(6级)位于顶部 (必需)" 2) 身份验证授权为向用户提交PUT请求的错误用户#更新操作 失败/错误:指定{response.should_重定向到(根路径)} 预期响应是重定向到>http://www.example.com/ 但是是重定向到>https://www.example.com/users/49 #./spec/requests/authentication\u pages\u spec.rb:77:in'block(5级)in'top (必需)" 以下是认证规范,其中两个失败的测试来自: 需要“spec\u helper”Ruby on rails RubyonRails教程:第9.2章测试失败,ruby-on-rails,rspec,rspec-rails,railstutorial.org,Ruby On Rails,Rspec,Rspec Rails,Railstutorial.org,我正在学习Michael Hartl教程的最新版本,但我无法通过第9.2章中的几个测试: 我已经验证了我的gem版本,重新启动了Rails服务器,运行了bundle更新,并重建了测试数据库,但都没有用。我已经从git存储库中重新编译了,并且浏览了我认为相关的每一行。我在第9章中没有遇到任何问题,但我正在努力深入,尤其是像这一章这样的web安全部分,因为我想在完成教程后使用此模型创建一个新网站。非常感谢您的帮助 作为旁注,编辑重定向似乎工作正常,但是使用PUT的测试失败了,即使它们在控制器中使用
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
以下是用户控制器:
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
我想出来了。这是因为我在应用程序控制器中打开了SSL。我添加了一个环境测试,现在一切都通过了
class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper
if Rails.env.production?
force_ssl
end
end
class ApplicationController
您从哪个提交进行复制?我已经检查过了,但是没有注意到你发布的文件有任何问题,所以问题可能出在其他地方,这就是我正在使用的版本,第二版:做得好。请将此问题标记为已解决,以免出现在未回答问题列表中。
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
class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper
if Rails.env.production?
force_ssl
end
end