2013-06-20 107 views
0

我目前正在通過Michael Hartl的Rails Tutorial工作,並且在他應該說的時候一直無法進行rspec測試工作。這些是列表8.10之後的測試。這是在登入登出章以及麻煩的認證測試方法如下:Michael Hartl的Rails教程第8章中的問題 - CSS預計

require 'spec_helper' 

describe "Authentication" do 

    subject { page } 


    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 

end 

我相信這兩個描述簽到測試應該通過,但「它{應當have_selector('div.alert .alert錯誤」,文本:‘無效’)}」測試當前顯示:

Failure/Error: it { should have_selector('div.alert.alert-error', text: 'Invalid') } 
    expected css "div.alert.alert-error" with text "Invalid" to return something 
# ./spec/requests/authentication_pages_spec.rb:21:in `block (4 levels) in <top (required)>' 

我的會話控制器如下:

class SessionsController < ApplicationController 

    def new 
    end 

def create 
    user = User.find_by_email(params[:session][:email]) 
    if user && user.authenticate(params[:session][:password]) 
     #I haven't gotten to this tutorial part yet 
    else 
     flash.now[:error] = "Invalid email/password combination" 
     render 'new' 
    end 
    end 

    def destroy 
    end 
end 

當我測試這與內置的Rails的服務器,b rowser源代碼包含:

<!--[if lt IE 9]> 
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> 
<![endif]-->  
    </head> 
    <body> 
    <header class="navbar navbar-fixed-top navbar-inverse"> 
    <div class="navbar-inner"> 
    <div class="container"> 
     <a href="/" id="logo">sample app</a> 
     <nav> 
     <ul class="nav pull-right"> 
      <li><a href="/">Home</a></li> 
      <li><a href="/help">Help</a></li> 
      <li><a href="#">Sign in</a></li> 
     </ul> 
     </nav> 
    </div> 
    </div> 
</header> 
    <div class="container"> 
     <div class="alert alert -error">Invalid email/password combination</div> 
     <h1>Sign in</h1> 

<div class="row"> 
    <div class="span6 offset3"> 
    <form accept-charset="UTF-8" action="/sessions" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="Gh9r4Qf0R00A31u69ETFKeAt57nj6Qqai5iuBISWOWE=" /></div> 

     <label for="session_email">Email</label> 
     <input id="session_email" name="session[email]" size="30" type="text" /> 

     <label for="session_password">Password</label> 
     <input id="session_password" name="session[password]" size="30" type="password" /> 

     <input class="btn btn-large btn-primary" name="commit" type="submit" value="Sign in" /> 
</form> 

我想,對於我而言,臨界線這裏是:

<div class="container"> 
    <div class="alert alert -error">Invalid email/password combination</div> 
    <h1>Sign in</h1> 

我認爲應該發生的事情是,我的測試訪問此頁,並應看到在生成的html中存在警報警告 - 錯誤分類(具有可接受的文本),並通過測試。我知道這不會發生,但我確實有一個問題,就是我對這個過程的高層理解是否正確。

我一直在尋找其他教程問題的計算器。雖然也有類似的問題,但我還沒有能夠用它來回答我的問題,所以我在問這個問題,因爲我認爲這是一個新問題。我確實看到教程問題提供了很多文件,而我只是試圖在這裏發佈relavent文件。然而,這裏是我當前的routes.rb文件

SampleApp::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 

這裏是我目前的new.html.erb文件,該文件在〜/ rails_projects/sample_app /應用/視圖/會話/ new.html文件路徑。 erb

<% provide(:title, "Sign in") %> 
<h1>Sign in</h1> 

<div class="row"> 
    <div class="span6 offset3"> 
    <%= form_for(:session, url: sessions_path) do |f| %> 

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

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

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

    <p>New user? <%= link_to "Sign up now!", signup_path %></p> 
    </div> 
</div> 

我打算繼續學習本教程,因爲它看起來像功能沒有阻礙。不過,我擔心後來我在這些測試中缺乏理解會嚴重干擾事情。如果有人遇到類似問題,寒冷的幫助將我指向一個解決方案,那將是非常棒的。

在此先感謝!

回答

1

從您的網頁源代碼檢查:

<div class="alert alert -error">Invalid email/password combination</div> 

目標div有類alert-error。然而,在你的測試用例,你期待一個div alert類和alert-error

它{應當have_selector( 'div.alert.alert錯誤',文本: '無效')}

我想你在alert-error之間輸入了一個額外的空格,它應該是alert-error(沒有空格)。

你應該檢查你的佈局視圖,因爲你粘貼的sessions/new.html.erb不包括產生這行HTML的源代碼。

UPDATE:如Dylan Markow所述,您可能在<div class="alert alert-<%= key %>">中鍵入了額外的空格。

+1

具體來說,比較清單7.26到您的實際佈局('

<%= value %>
'部分) –

+0

迪倫,感謝您告訴我具體的上市,它真的讓事情變得更容易。關於rails的難題,作爲一個新用戶來講,就是有很多文件。 – Nathan

+0

非常感謝。你們真棒,所有的測試(呃,那些在......之前失敗的測試)現在都過去了。所以,爲了澄清,文件app/views/layouts/application.html.erb是瀏覽器看起來生成html的地方?或者這個文件中的html最終以何種方式傳遞給瀏覽器? – Nathan

相關問題