assert_select count预期精确匹配2个元素,匹配“ a [href =” /“]”,找到3

问题描述 投票:0回答:1

我正在尝试检查有多少链接路由到root_path。但是它一直给3。我尝试将所有的root_path都取出来,检查我是否错过了任何拼写,但对我来说看起来还不错。

错误

01:11:53 - INFO - Running: test/integration/site_layout_test.rb
Running via Spring preloader in process 88114
Started with run options --seed 8624

 FAIL["test_layout_links", #<Minitest::Reporters::Suite:0x00007ffebeae7648 @name="SiteLayoutTest">, 1.9762050000135787]
 test_layout_links#SiteLayoutTest (1.98s)
        Expected exactly 2 elements matching "a[href="/"]", found 3..
        Expected: 2
          Actual: 3
        test/integration/site_layout_test.rb:8:in `block in <class:SiteLayoutTest>'

  1/1: [==================================================================================] 100% Time: 00:00:01, Time: 00:00:01

Finished in 2.02235s
1 tests, 2 assertions, 1 failures, 0 errors, 0 skips

这是我所有需要的页面的代码

IntegrationTest

require 'test_helper'

class SiteLayoutTest < ActionDispatch::IntegrationTest

  test "layout links" do
    get root_url
    assert_template 'static_pages/home'
    assert_select "a[href=?]", root_path, count: 2
    assert_select "a[href=?]", help_path, count: 1
    assert_select "a[href=?]", about_path, count: 1
    assert_select "a[href=?]", contact_path, count: 1
  end
end

_ header

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

_ footer.html.erb

<footer class="footer">
  <small>
    The <a href="https://www.railstutorial.org/">Ruby on Rails Tutorial</a>
    by <a href="https://www.michaelhartl.com/">Michael Hartl</a>
  </small>
  <nav>
    <ul>
      <li><%= link_to "About",   about_path %></li>
      <li><%= link_to "Contact", contact_path %></li>
      <li><a href="https://news.railstutorial.org/">News</a></li>
    </ul>
  </nav>
</footer>

application.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title><%= full_title(yield(:title)) %></title>
    <%= render 'layouts/rails_default' %>
    <%= render 'layouts/shim' %>
  </head>
  <body>
    <%= render 'layouts/header' %>
    <div class="container">
      <%= yield %>
      <%= render 'layouts/footer' %>
    </div>
  </body>
</html>

routes.rb

Rails.application.routes.draw do
  root 'static_pages#home'
  get  '/help',    to: 'static_pages#help'
  get  '/about',   to: 'static_pages#about'
  get  '/contact', to: 'static_pages#contact'
end

Home.html.erb

<div class="center jumbotron">
  <h1>Welcome to the Sample App</h1>

  <h2>
    This is the home page for the
    <a href="https://www.railstutorial.org/">Ruby on Rails Tutorial</a>
    sample application.
  </h2>

   <%= link_to "Sign up now!", signup_path, class: "btn btn-lg btn-primary" %>
</div>
  <%= link_to image_tag("rails.svg", alt: "Rails logo", width: "200"),
                        "https://rubyonrails.org/" %>
  <%= link_to image_tag("kitten.jpg", alt: "kitten", width: "200") %>

全局root_path搜索Global root_path search result

ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-5 railstutorial.org
1个回答
0
投票

问题在第13章中得到了单独解决,即使我没有更改与该问题相关的任何内容。

© www.soinside.com 2019 - 2024. All rights reserved.