【问题标题】:Rspec testing view test fails but HTML is OK?Rspec测试视图测试失败但HTML没问题?
【发布时间】:2014-05-11 04:57:17
【问题描述】:

使用 Rspec,我正在测试导航中徽标的存在:

#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>

页面正常,但测试失败:

$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 行为页面是正确的,所以我认为我的测试不能正常工作。你能帮助我吗?

【问题讨论】:

    标签: ruby-on-rails ruby rspec


    【解决方案1】:

    替换

    should have_link("/")
    

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

    have_link取链接的display text或图片的alt属性值,使用href选项可以指定对应的路径。

    【讨论】:

      猜你喜欢
      • 2015-07-08
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多