2014-01-23 74 views
0

我討厭繼續包裹每個人,但我遇到了另一個問題。我想我只是錯過了一些非常簡單的東西,但我不能找到它會是什麼。我在第8章做了登錄/註冊/結束示例應用程序。但是當我運行測試時,我收到以下錯誤。添加下拉菜單後無法讓我的測試通過

Failures: 

    1) Aithentication signin with valid information followed by signout 
    Failure/Error: before { click_link "Sign Out" } 
    Capybara::ElementNotFound: 
     Unable to find link "Sign Out" 
    # ./spec/requests/authentication_pages_spec.rb:44:in `block (5 levels) in <top (required)>' 

    2) User pages signup page after saving the user 
    Failure/Error: it { should have_selector('div.alert.alert-success', text: 'Welcome') } 
     expected #has_selector?("div.alert.alert-success", {:text=>"Welcome"}) to return true, got false 
    # ./spec/requests/user_pages_spec.rb:53:in `block (4 levels) in <top (required)>' 

    3) User pages signup page after saving the user 
    Failure/Error: it { should have_title(user.name) } 
    NoMethodError: 
     undefined method `name' for nil:NilClass 
    # ./spec/requests/user_pages_spec.rb:52:in `block (4 levels) in <top (required)>' 

    4) User pages signup page after saving the user 
    Failure/Error: it { should have_link('Sign out') } 
     expected #has_link?("Sign out") to return true, got false 
    # ./spec/requests/user_pages_spec.rb:51:in `block (4 levels) in <top (required)>' 

Finished in 0.66029 seconds 
50 examples, 4 failures 

Failed examples: 

rspec ./spec/requests/authentication_pages_spec.rb:45 # Aithentication signin with valid information followed by signout 
rspec ./spec/requests/user_pages_spec.rb:53 # User pages signup page after saving the user 
rspec ./spec/requests/user_pages_spec.rb:52 # User pages signup page after saving the user 
rspec ./spec/requests/user_pages_spec.rb:51 # User pages signup page after saving the user 

這裏是我的User_pages_spec

require 'spec_helper' 

describe "User pages" do 

    subject { page } 

    describe "profile page" do 
    let(:user) { FactoryGirl.create(:user) } #this was what I added 
    before { visit user_path(user) } 

    it { should have_content(user.name) } 
    it { should have_title(user.name) } 
    end 

    describe "signup page" do 
    before { visit signup_path } 

    it { should have_content('Sign up') } 
    it { should have_title(full_title('Sign up')) } 
    end 

    describe "signup page" 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 
    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 
    end 

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

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

和我authentication_page_spec

require 'spec_helper' 

describe "Aithentication" do 

    subject { page } 

    describe "signin page" do 
     before { visit signin_path } 

     it { should have_content('Sign in') } 
     it { should have_title('Sign in') } 
    end 

    describe "signin" do 
     before { visit signin_path } 

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

      it { should have_title('Sign in') } 
      it { should have_selector('div.alert.alert-error') } 

      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.upcase 
     fill_in "Password", with: user.password 
     click_button "Sign in" 
     end 

     it { should have_title(user.name) } 
     it { should have_link('Profile',  href: user_path(user), visible: false) } 
     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 

這裏是我的_header其中大部分故障都來自我認爲

<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", '#' %></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", '#' %></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> 

讓我知道你是否需要d其他任何感謝您的幫助。

回答

1

水豚通常搜索區分大小寫,並搜索帶有標籤,名稱或ID的字段。

在第一個測試用例

<%= link_to "Sign out", signout_path, method: "delete" %> 

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

在這裏,您正在搜索是有Sign outSign Out和鏈接。

而關於第三個測試用例

let(:user) { User.find_by(email: '[email protected]') }改變這

let(:user) { User.find_by_email('[email protected]') } 

希望這將解決您的問題。