2014-03-31 44 views
1

使用RSpec,我測試的navnar標誌的存在:Rspec測試視圖測試失敗,但HTML可以嗎?

#spec/views/_header_spec.html.erb 
require 'spec_helper' 

describe "layouts/_header.html.erb" do 
    subject{rendered} 


    it "should have the clickable logo" do 
    render 
    should have_selector("img") 
    should have_link("/") 
    end 
end 

這是我生成的HTML:

<a href="/" class="navbar-brand"> 
    <img alt="Logo" src="/assets/logo.png"> 
</a> 

的頁面是OK,但測試失敗:

$rspec spec/views/_header_spec.rb 
F 

Failures: 

    1) layouts/_header.html.erb should have the clickable logo 
    Failure/Error: should have_link("/") 
    Capybara::ExpectationNotMet: 
     expected to find link "/" but there were no matches 
    # ./spec/views/_header_spec.rb:10:in `block (2 levels) in <top (required)>' 

Finished in 0.13914 seconds 
1 example, 1 failure 

Failed examples: 

rspec ./spec/views/_header_spec.rb:7 # layouts/_header.html.erb should have the clickable logo 

Randomized with seed 37707 

測試失敗,但HTML行爲頁面是正確的,所以我認爲我的測試工作不正常。你可以幫我嗎?

回答

2

更換

should have_link("/") 

隨着

should have_link("Logo", href: "/") 

have_link需要鏈接或圖像的alt屬性值,並且使用href選項,您可以指定相應的路徑display text

+0

謝謝。這個對我有用。 – user1066183

+0

很高興幫助:) –