2013-01-25 127 views
0

這裏是我的測試:爲什麼我找不到h1標籤?

require "rspec" 

describe HomeController do 
    render_views 

    it "should renders the home" do 
    get :home 
    response.should render_template("home") 
    find('H1').should have_content("Simulate Circuits Online") 

    end 

end 

,但我得到:

1) HomeController should renders the home 
    Failure/Error: find('H1').should have_content("Simulate Circuits Online") 
    Capybara::ElementNotFound: 
     Unable to find css "H1" 
    # ./spec/controllers/home_controller_spec.rb:9:in `block (2 levels) in <top (required)>' 
    # (eval):6:in `block in fork' 

如何找到一個標籤,或ID,或CSS?

回答

2

不能對你要求的方法,如getpost等。水豚的匹配得到響應使用水豚的匹配,你必須使用visit,如visit 'home'

有關詳細信息,請參閱this post。此報價尤其如下:

作爲驗收測試框架,Capybara不會公開底層細節,如請求或響應對象。爲了使用Capybara訪問網頁,開發者需要使用方法訪問(而不是get)。要閱讀訪問的頁面主體,開發人員必須使用頁面而不是操縱響應。

也看到這個職位:Check page response on multiple pages with Capybara

如果你想測試一個HTML串響應的結構,你可以考慮rspec-html-matchers

+0

很好的答案..謝謝 – simo

+0

但如何訪問一個動作,而不硬編碼呢? – simo

+1

您可以使用例如'訪問home_path',假設在你的路由中定義了'home_path'。 –