2013-01-25 29 views
4

這裏是我的測試:如何在渲染視圖上測試文本?

require "rspec" 

describe HomeController do 
    render_views 

    it "should renders the home" do 
    get :home 
    response.should render_template("home") 
    response.should include_text("Simulate Circuits Online") 
    end 

end 

但是,我得到:

1) HomeController should renders the home 
    Failure/Error: response.should include_text("Some text to be test ..") 
    NoMethodError: 
     undefined method `include_text' for #<RSpec::Core::ExampleGroup::Nested_1:0xd429a0c> 
    # ./spec/controllers/home_controller_spec.rb:9:in `block (2 levels) in <top (required)>' 
    # (eval):6:in `block in fork' 
    # (eval):6:in `fork' 
    # (eval):6:in `fork' 

那麼,什麼是測試渲染文本的正確方法?

編輯 如果我嘗試response.should have_content("My text ..")

我得到

1) HomeController should renders the home 
    Failure/Error: response.should have_content("My text ..") 
     expected there to be text "My text .." in "#" 
    # ./spec/controllers/home_controller_spec.rb:9:in `block (2 levels) in <top (required)>' 

回答

3

在您的視圖模板實際上,你有你檢查的文本?錯誤似乎是它實際上找不到文本。

此外,請嘗試使用response.should have_text

1

試試這個:

response.should have_content("Simulate Circuits Online") 
+0

請參閱編輯 – simo

+0

我搜索了RSpec的文本匹配器,但是我找不到任何文本,請問我可以在哪裏閱讀? – simo

0

您還可以使用DOM元素的屬性和內容。

expect(page).to have_selector 'h1', :text => 'Your text here' 

此方法不檢索任何棄用警告。