2013-05-27 45 views
1

我通過邁克·哈特爾的Rails的教程和in section 4.4緩慢的看來,它讓我從格式改變RSpec的請求文件:水豚未定義的方法'has_title?'?

page.should have_selector('title', :text => "#{base_title}") 

expect(page).to have_title("Ruby on Rails Tutorial Sample App") 

現在我得到兩個未定義的方法錯誤,我使用「.to have_title」和「.to_not have_title」。我關閉並重新啓動了Webrick,Spork和Guard以防萬一,但它仍然無效。

水豚1.1.2版本 Rspec的版本2.11.1

請讓我知道是否需要任何其他信息。

+0

這可能會有所幫助,特別是如果您已升級到Capybara 2.1,如您在回答的評論中指出的那樣:[RSpec&Capybara 2.0絆倒我的have_selector測試](http://stackoverflow.com/q/13573525/ 567863) –

回答

4

顯然,教程已經改變了最近。

訪問通過谷歌緩存頁面顯示的原始版本(這對我來說工作正常):

require 'spec_helper' 

describe "Static pages" do 

    describe "Home page" do 

    it "should have the h1 'Sample App'" do 
     visit '/static_pages/home' 
     page.should have_selector('h1', :text => 'Sample App') 
    end 

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

    it "should not have a custom page title" do 
     visit '/static_pages/home' 
     page.should_not have_selector('title', :text => '| Home') 
    end 
    end 
    . 
    . 
    . 
end 
+0

所以這個絕對有訣竅,當我最近重新加載4.4節的頁面時,它實際上有你的修正版本,你粘貼在這裏。 Mike Hartl是否從昨天開始修復它?我覺得我會有點瘋狂...... – Haltingpoint

+0

另外,只需要注意static_pages_spec.rb文件頂部的代碼缺少'let(:base_title){「Ruby on Rails教程示例應用程序」}'在「描述」靜態頁面「do」和「描述」主頁「do」之間。 – Haltingpoint

0

可能是您的版本的問題。通過https://github.com/jnicklas/capybara/issues/863

expect(first('title').native.text).to eq "my title" 
+0

我很困惑 - 我沒有使用「第一」的方法... 此外,fwiw,我使用水豚1.1.2以及2.1.0測試,並有相同的結果。 – Haltingpoint

+0

試試這個page.should have_xpath(「// title」,:text =>「我的標題」) –

0

我有這個問題,我意識到,最終我已經切換到下面的導軌4版的代碼,而比3.2。

來測試你使用的是什麼版本的軌道,教程頁面上鍵入命令

rails -v 

然後,使用導航鍵在右側選擇正確的軌道版本。看起來很簡單,但希望這會爲他人解決這個問題帶來的痛苦。

相關問題