2012-07-31 53 views
4

_header.html.erb發現Rails的LINK_TO不受水豚

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

user_pages_spec.rb

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 

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

失敗是:

1) User pages signup with valid information followed by signout 
Failure/Error: before (click_link "Sign out") 
Capybara::ElementNotFound: 
no link with title, id, or text 'Sign out' found 
(eval):2:in 'click_link' 
./spec/requests/user_pages_spec.rb:63:in 'block (5 levels) in (top (required))' 

繼創建用戶過程中手動看來上班。鏈接在下拉菜單中,如果這可能與它有關係?這是MHartl rails教程中的一個點,他說所有的測試都應該通過。

回答

11

可疑代碼顯然需要被嵌套在前面的描述裏面

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

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

感謝它爲我工作在同一個錯誤 – dnlcrl 2012-11-22 00:34:13

3

最後描述中的before塊尚未創建用戶。這就是爲什麼用戶(尚不存在)目前尚未登錄,因此沒有「登出」鏈接。

一個可能的解決方案是click_button 'submit'之前的塊。另一種方法是完全跳過before區塊的註銷,因爲此時沒有用戶登錄。

0

是的,它應該嵌套在前面的描述。我可以確認這也適用於本教程的Rails 4版本。