2012-09-30 34 views
2

我正在通過Michael Hartl的Ruby on Rails教程開展工作,我正在做第3章練習。有人可以解釋爲什麼這個測試失敗了嗎?Rspec測試失敗:靜態頁面關於頁面應該有標題'關於我們'Hartl Ch。 3

我得到的失敗

rspec ./spec/requests/static_pages_spec.rb:39 # 
Static pages About page should have the title 'About Us' 

控制器

class StaticPagesController < ApplicationController 
    def home 
    end 

    def help 
    end 

    def about 
    end 

    def Contact 
    end 
end 

About.html.erb

<!DOCTYPE html> 
<html> 
<head> 
<title>Ruby on Rails Tutorial Sample App | About Us</title> 
</head> 
<body> 
<h1>About Us</h1> 

Spec.rb

describe "About page" do 

    it "should have the h1 'About Us'" do 
    visit '/static_pages/about' 
    page.should have_selector('h1', :text => 'About Us') 
end 
it "should have the title 'About Us'" do 
    visit '/static_pages/about' 
    page.should have_selector('title', 
       :text => "Ruby on Rails Tutorial Sample App | About Us") 
    end 
end 

的routes.rb

SampleApp::Application.routes.draw do 
    get "static_pages/home" 

    get "static_pages/help" 

    get "static_pages/about" 

    get "static_pages/Contact" 
end 

回答

0

嘗試

page.should have_xpath("//title", :text => "About Us") 
+0

這工作!謝謝。你能幫我理解它是如何工作的嗎? –

+1

我想如果你使用的是have_selector,試着用如下內容替換文本:'page.should have_selector('h1',:content =>'About Us')'。其實我建議使用[capybara](https://github.com/jnicklas/capybara)來測試你的視圖。來自Rspec的have_selector從中學習到。這對我來說更容易使用。 –

0

如果您遵循MHartl的教程,你正在使用的寶石 '水豚',我可以確認,更改「:文本=>。 ..'to':content => ...'會讓你的測試通過。感謝@Kuo Jimmy