预计恰好2个元素匹配“a [href =”/“]”,找到0

时间:2016-10-29 12:57:53

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4

我正在尝试检查有多少链接路由到root_path。我的问题是为什么我的_header.html.erb文件中的路由不被assert_select计算?

(我是代码和rails的初学者,我正在关注Michael Hartl的教程)

root_path在页面中使用了两次:

 <%= link_to "sample app", root_path, id: "logo" %>
 <li><%= link_to "Home",    root_path %></li>

以下是我的集成测试代码:

require 'test_helper'
class SiteLayoutTest < ActionDispatch::IntegrationTest
  test "layout links" do
    get root_path
    assert_template 'static_pages/home'
    assert_select "a [href=?]", root_path, count: 2
    assert_select "a [href=?]", help_path
    assert_select "a [href=?]", about_path
    assert_select "a [href=?]", contact_path
  end
end

这是我的HTML文件(_header.html.erb)的部分代码:

<header class="navbar navbar-fixed-top navbar-inverse">
  <div class="container">
    <%= link_to "sample app", root_path, id: "logo" %>
    <nav>
      <ul class="nav navbar-nav navbar-right">
        <li><%= link_to "Home",    root_path %></li>
        <li><%= link_to "Help",    help_path %></li>
        <li><%= link_to "Log in", '#' %></li>
      </ul>
    </nav>
  </div>
</header>

当我运行 $ bundle exec rake test:integration 时,它给了我1次失败:

FAIL["test_layout_links", SiteLayoutTest, 2016-10-20 16:03:19 +0000]
 test_layout_links#SiteLayoutTest (1476979399.42s)
        Expected exactly 2 elements matching "a [href="/"]", found 0..
        Expected: 2
          Actual: 0
        test/integration/site_layout_test.rb:8:in `block in <class:SiteLayoutTest>'

1 个答案:

答案 0 :(得分:1)

我发现了问题:不应该是&#34; a&#34;之间的空间。和集成测试代码中的括号。 (这个错误花了我半天......)