3

我繼續收到相同的故障消息,但我似乎無法弄清楚什麼是錯的。第9章,Hartl ROR第9.2.2節

失敗:

1)在用戶控制非登錄用戶訪問編輯頁面 故障/錯誤認證授權:它{應have_selector(「標題」,文本:「登錄」) } 預期的CSS「標題」文本「登錄」到「

2)認證,授權返回的東西 #./spec/requests/authentication_pages_spec.rb:69:in`塊(6級)的非用戶控制器中的登錄用戶提交更新操作 失敗/錯誤:指定{response.should redirect_to(signin_path)} 預期響應是一個<:重定向>,但在'

是< 200> #./spec/requests/authentication_pages_spec.rb:74:in`塊(6級)的2.31秒 69個實例成品,2次失敗

失敗的例子:

rspec的用於./spec/requests/authentication_pages_spec.rb:69#認證授權未登入在用戶控制用戶訪問的編輯頁面 rspec的./spec/ requests/authentication_pages_spec.rb:74#未登錄使用的身份驗證授權在用戶控制器RS提交更新操作

這裏是authentication_pages_spec.rb: 需要 'spec_helper'

形容 「認證」 做

subject { page } 

describe "signin page" do 
    before { visit signin_path } 

    it { should have_selector('h1', text: 'Sign in') } 
    it { should have_selector('title', text: 'Sign in') } 
end 

describe "signin" do 
    before { visit signin_path } 

    describe "with invalid information" do 
     before { click_button "Sign in" } 

     it { should have_selector('title', text: 'Sign in') } 
     it { should have_selector('div.alert.alert-error', text: 'Invalid') } 

     describe "after visiting another page" do 
      before { click_link "Home" } 
      it { should_not have_selector('div.alert.alert-error') } 
     end 
    end 

    describe "with valid information" do 
     let(:user) { FactoryGirl.create(:user) } 
     before do 
      fill_in "Email", with: user.email 
      fill_in "Password", with: user.password 
      click_button "Sign in" 
     end 

     it { should have_selector('title', text: user.name) } 
     it { should have_link('Profile', href: user_path(user)) } 
     it { should have_link('Sign out', href: signout_path) } 
     it { should_not have_link('Sign in', href: signin_path) } 

     describe "followed by signout" do 
      before { click_link "Sign out" } 
      it { should have_link('Sign in') } 
     end 
    end 
end 

describe "with valid information" do 
    let(:user) { FactoryGirl.create(:user) } 
    before {sign_in user } 

    it { should have_selector('title', text: user.name) } 
    it { should have_link('Profile', href: user_path(user)) } 
    it { should have_link('Settings', href: edit_user_path(user)) } 
    it { should have_link('Sign out', href: signout_path) } 
    it { should_not have_link('Sign in', href: signin_path) } 
end 

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) } 
      end 
     end 
     end 
end 
end 

這裏是user_pages_spec.rb

要求'spec_helper'

描述「用戶頁面」做

subject { page } 

describe "signup page" do 
    before { visit signup_path } 

    it { should have_selector('h1', text: 'Sign up') } 
    it { should have_selector('title', text: 'Sign up') } 
end 

describe "signup" do 

    before { visit signup_path } 

    let(:submit) { "Create my account" } 

    describe "with invalid information" do 
     it "should not create a user" do 
      expect { click_button submit }.not_to change(User, :count) 
     end 

     describe "error messages" do 
      before { click_button submit } 

      it { should have_selector('title', text: 'Sign up') } 
      it { should have_content('error') } 
     end 
    end 

    describe "with valid information" do 
     before do 
      fill_in "Name", with: "Example User" 
      fill_in "Email", with: "[email protected]" 
      fill_in "Password", with: "foobar" 
      fill_in "Confirmation", with: "foobar" 
     end 

     it "should create a user" do 
      expect { click_button submit }.to change(User, :count).by(1) 
     end 

     describe "after saving the user" do 
      before { click_button submit } 
      let(:user) { User.find_by_email('[email protected]') } 

      it { should have_selector('title', text: user.name) } 
      it { should have_selector('div.alert.alert-success', text: 'Welcome') } 
      it { should have_link('Sign out') } 
     end 
    end 
end 

describe "profile page" do 
    let(:user) { FactoryGirl.create(:user) } 
    before { visit user_path(user)} 

    it { should have_selector('h1', text: user.name) } 
    it { should have_selector('title', text: user.name) } 
end 

describe "edit" do 
    let(:user) { FactoryGirl.create(:user) } 
    before do 
     sign_in user 
     visit edit_user_path(user) 
    end 

    describe "page" do 
     it { should have_selector('h1', text: "Update your profile") } 
     it { should have_selector('title', text: "Edit user") } 
     it { should have_link('change', href: 'http://gravatar.com/emails') } 
    end 

    describe "with invalid information" do 
     before { click_button "Save changes" } 

     it { should have_content('error') } 
    end 

    describe "with valid information" do 
     let(:new_name) { "New Name" } 
     let(:new_email) { "[email protected]" } 
     before do 
      fill_in "Name", with: new_name 
      fill_in "Email", with: new_email 
      fill_in "Password", with: user.password 
      fill_in "Confirm Password", with: user.password 
      click_button "Save changes" 
     end 

     it { should have_selector('title', text: new_name) } 
     it { should have_selector('div.alert.alert-success') } 
     it { should have_link('Sign out', href: signout_path) } 
     specify { user.reload.name.should == new_name } 
     specify { user.reload.email.should == new_email } 
    end 
    end 
end 

這裏是users_controller.rb:

class UsersController < ApplicationController 

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 
    @user = User.find(params[:id]) 
end 

def update 
    @user = User.find(params[:id]) 
    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_path, notice: "Please sign in." unless signed_in? 
end 
end 

這裏是edit.html.erb:

<% provide(:title, "Edit user") %> 
<h1>Update your profile</h1> 

<div class="row"> 
    <div class="span6 offset3"> 
    <%= form_for(@user) do |f| %> 
     <%= render 'shared/error_messages' %> 

     <%= f.label :name %> 
     <%= f.text_field :name %> 

     <%= f.label :email %> 
     <%= f.text_field :email %> 

     <%= f.label :password %> 
     <%= f.password_field :password %> 

     <%= f.label :password_confirmation, "Confirm Password" %> 
     <%= f.password_field :password_confirmation %> 

     <%= f.submit "Save changes", class: "btn btn-large btn-primary" %> 
    <% end %> 

    <%= gravatar_for @user %> 
    <a href="http://gravatar.com/emails">change</a> 
    </div> 
</div> 

這裏是utilities.rb:

def full_title(page_title) 
    base_title = "Ruby on Rails Tutorial Sample App" 
    if page_title.empty? 
     base_title 
    else 
     "#{base_title} | #{page_title}" 
    end 
end 

def sign_in(user) 
    visit signin_path 
    fill_in "Email", with: user.email 
    fill_in "Password", with: user.password 
    click_button "Sign in" 

    cookies[:remember_token] = user.remember_token 
end 

這裏是_header.html.erb:

<header class="navbar navbar-fixed-top"> 
    <div class="navbar-inner"> 
    <div class="container"> 
     <%= link_to "sample app", root_path, id: "logo" %> 
     <nav> 
     <ul class="nav pull-right"> 
      <li><%= link_to "Home", root_path %></li> 
      <li><%= link_to "Help", help_path %></li> 
      <% if signed_in? %> 
      <li><%= link_to "Users", '#' %></li> 
      <li id="fat-menu" class="dropdown"> 
       <a href="#" class="dropdown-toggle" data-toggle="dropdown"> 
       Account <b class="caret"></b> 
       </a> 
       <ul class="dropdown-menu"> 
       <li><%= link_to "Profile", current_user %></li> 
       <li><%= link_to "Settings", edit_user_path(current_user) %></li> 
       <li class="divider"></li> 
       <li> 
        <%= link_to "Sign out", signout_path, method: "delete" %> 
       </li> 
       </ul> 
      </li> 
      <% else %> 
      <li><%= link_to "Sign in", signin_path %></li> 
      <% end %> 
     </ul> 
     </nav> 
    </div> 
    </div> 
</header> 

這些是我在第9章中修改過的文件,但如果有其他人認爲可能是問題,請讓我知道,我可以發佈它們。謝謝!

+0

如果你刪掉不相關的東西,那麼閱讀你的問題就容易多了 - 不需要包含例如通過的規格 – 2012-07-08 19:43:12

+0

對不起!我仍然是初學者,所以我不能100%確定什麼是相關信息,什麼不是。 – user1410781 2012-07-08 20:01:40

回答

5

看起來你錯過了你的UsersController頂部的before_filter :signed_in_user, only: [:edit, :update]行。再看看Listing 9.12

+0

感謝您的幫助! – user1410781 2012-07-09 15:20:36