2012-09-19 94 views
2

我通过迈克尔·哈特尔的教程的最新版本,而我不能让一对夫妇从第9.2章测试通过:Ruby on Rails的教程:第一章9.2测试失败

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

我已经验证了我的gem版本,重新启动Rails服务器,运行捆绑更新和重建测试数据库无济于事。我已经从git仓库中复制,并且遍历了我认为相关的每一行。我还没有遇到任何麻烦向上穿过第9章,但我想成为彻底的,尤其是像这样的网络安全的部分,因为我想打一个新的网站采用这种模式,一旦我完成教程。任何帮助深表谢意。

作为一个方面说明,编辑重定向似乎工作正常,但使用PUT的测试失败,即使他们在控制器中使用相同的重定向功能,我不明白他们为什么会表现不同。再次感谢您的帮助。

约翰

失败消息:

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: "[email protected]") } 
     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 

下面是用户控制:

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 
+0

这是提交你抄袭?我曾经反对[这]检查(https://github.com/railstutorial/sample_app_2nd_ed/commit/9bbe3e844f5a5b366c46a80ec64ee0c214d7824d),但没有发现任何不妥您已发布的文件,因此可能问题已经躺在别的地方 – prusswan

+0

是,这是我与,第二版使用的版本: https://github.com/railstutorial/sample_app_2nd_ed –

回答

3

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

class ApplicationController < ActionController::Base 
    protect_from_forgery 
    include SessionsHelper 

    if Rails.env.production? 
    force_ssl 
    end 
end 
+0

不错的工作。请将此问题标记为已解决,因此不会出现在未解答的问题列表中。 –

相关问题