2013-05-12 31 views
0

儘管我編寫了第一組錯誤消息,但仍然停留在編輯用戶章9.1.1測試中 - 感謝你們的幫助。 在我所在的同一地點出現了一個新的測試失敗錯誤,我被鼓勵提出一個新問題。我會把我的更新代碼放在這裏。Rspec測試失敗Hartl教程Chap。 9.1 - 編輯用戶標題失敗

我正在運行此測試:$ bundle exec rspec spec/requests/user_pages_spec.rb -e「編輯頁面」 我只是沒有看到錯誤來自哪裏,經過大量搜索。 我更新了2.0.0水豚也

Failures: 

    1) User pages signup edit page 
    Failure/Error: it { should have_selector('title', text: "Edit user") } 
    Capybara::ExpectationNotMet: 
     expected to find css "title" with text "Edit user" but there were no matches. Also found "",   which matched the selector but not all filters. 
    # ./spec/requests/user_pages_spec.rb:60:in `block (5 levels) in <top (required)>' 

Finished in 0.56475 seconds 
3 examples, 1 failure 

這裏的用戶頁面規格:

require 'spec_helper' 

describe "User pages" do 

    subject { page } 

    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 "signup page" do 
    before { visit signup_path } 

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

describe "signup" do 

    before { visit signup_path } 

    describe "with invalid information" do 
     it "should not create a user" do 
     expect { click_button submit }.not_to change(User, :count) 
     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 } 
     it { should have_link('Sign out') } 
    end 
     end 
    end 

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

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

這裏是我修改用戶控制器:

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 Your App!" 
     redirect_to @user 
    else 
     render 'new'  
     end 
    end 

    def edit 
    @user = User.find(params[:id]) 
    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, "Confirmation" %> 
     <%= 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> 

這裏是認證規格:

require 'spec_helper' 

describe "Authentication" do 

    subject { page } 

    describe "signin page" do 
    before { visit signin_path } 

    it { should have_selector('h1', text: 'Sign in') } 
    it { should have_selector('title', text: full_title('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('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)} 


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

和標題html.erb文件

<header class="navbar navbar-fixed-top navbar-inverse"> 
<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", users_path %></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> 
+0

嘗試'content_for:標題{ '修改用戶'}' – 2013-05-12 03:11:40

+0

<%= content_for(:標題, 「編輯用戶」)%>此線沒不會更改錯誤 – PatrickLightning 2013-05-12 03:12:31

+0

此外,標題標記未關閉,但可能是一個片段。標題標籤在原文中爲 – 2013-05-12 03:17:45

回答

0

看起來你可以有你的描述塊嵌套錯誤。

「用戶頁面立即登記編輯頁面」

這表明編輯頁面嵌套在「註冊」,它應該是在用戶的頁面。

這可以使歷境是錯誤

require 'spec_helper' 

describe "User pages" do 

    subject { page } 

    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 "signup page" do 
    before { visit signup_path } 

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

    describe "signup" do 

    before { visit signup_path } 

    describe "with invalid information" do 
     it "should not create a user" do 
     expect { click_button submit }.not_to change(User, :count) 
     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 } 
      it { should have_link('Sign out') } 
     end 
     end 
    end 
    end 


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

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

謝謝@muttonlamb我會研究它。你在哪裏推薦學習Rails約定中的嵌套? – PatrickLightning 2013-05-12 04:18:56

+0

這與嵌套在rails中沒有多大關係,它是關於如何在RSpec中設置測試上下文的。描述塊的各個級別設置不同的上下文。我已經更新了答案以幫助 - 基本上我認爲你的做,結束壞了 – muttonlamb 2013-05-12 04:34:22

+0

爲了在用戶頁面下嵌套編輯頁面,是否需要更改縮進或移動描述部分(最後16行左右)向上?是否需要以不同的順序放置這些塊?我試圖在github上學習hartl的代碼。 – PatrickLightning 2013-05-12 05:32:03

相關問題