2014-03-31 45 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

很高兴帮助:) –