【问题标题】:Rails Tutorial test_layout_links test failureRails 教程 test_layout_links 测试失败
【发布时间】:2015-02-14 20:23:53
【问题描述】:

我一直在做 Harl 教程,这个问题并没有造成大问题,但我不明白为什么我的测试找不到“联系人”页面的链接。任何指向正确方向的指针都表示赞赏。

Controller:
class StaticPagesController < ApplicationController
 def home
 end

 def help
 end

 def about
 end

 def contact
 end 
end

site_layout_test.rb
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
  assert_select "a[href=?]", help_path
  assert_select "a[href=?]", contact_path
  assert_select "a[href=?]", about_path
  end
end

routes.rb 
Rails.application.routes.draw do

root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new' 
resources :users 

end

这是链接的唯一位置,在

_footer.html.erb
<footer class="footer">
  <small>
    The <a href="http://www.railstutorial.org/">Ruby on Rails Tutorial</a> by <a href="http://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="http://railstutorial.org/">News</a></li>
    </ul>
</nav>

这是我收到的错误消息:

1) Failure:
  SiteLayoutTest#test_layout_links [/home/will/rails/sample_app/test/integration/site_layout_test.rb:9]:
  Expected at least 1 element matching "a[href="/contact"]", found 0..
  Expected 0 to be >= 1.

这是联系模板:

联系方式

在联系页面上联系关于示例应用程序的 Ruby on Rails 教程。

这是 application.html.erb 的布局

<!DOCTYPE html>
 <html>
 <head>
 <title><%= full_title(yield(:title)) %></title>
 <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
 <%= csrf_meta_tags %>
 <%=  render 'layouts/shim' %>
 </head>
 <body>
    <%= render 'layouts/header' %>
    <div class="container">
        <%= yield %>
        <%= render 'layouts/footer' %>
        <%= debug(params) if Rails.env.development? %>
    <div>
 </body>
 </html>

【问题讨论】:

  • 也向我们展示您的模板文件。
  • application.html.erb 模板?联系页面模板?
  • 如果您导航到该页面,联系链接是否会显示在您的浏览器中?
  • 是的,如果我单击本地主机上运行的链接,我会在地址栏中获得 /contact。
  • 在您的测试中get root_path 之后,添加puts response.body 并将结果添加到答案中。你也可以添加你的布局文件。

标签: ruby-on-rails railstutorial.org


【解决方案1】:

在您的测试中get root_path 之后,添加puts response.body 并将结果添加到答案中。你也可以添加你的布局文件。

【讨论】:

    【解决方案2】:

    刚刚解决了这个类似的错误 - 但是我的错误是在“关于”页面上。

    从您的 contact.html.erb 文件中删除 !DOCTYPE html 和任何其他 html。

    运行测试,一切顺利!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多