我正在完成本教程并且我的网站链接正常工作,但唯一没有通过的site_layout链接测试是/ about path。我检查了我的路线文件,页脚文件和site_layout_test,似乎无法找到什么错误
1) Failure:
SiteLayoutTest#test_layout_links [/home/ubuntu/workspace/sample_app/test/integration/site_layout_test.rb:10]:
Expected at least 1 element matching "a[href="/about"]", found 0..
Expected 0 to be >= 1.
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
<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://news.railstutorial.org/">News</a></li>
</ul>
</nav>
</footer>
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
答案 0 :(得分:1)
这似乎是云9 ide的常见错误。我仔细检查了每个视图文件,当只有2时,我的集成测试会找到3个指向root_path的链接。如果你在本地rails控制台内的本地应用程序上运行测试,它们就能工作。 c9的控制台不正确。
答案 1 :(得分:0)
我刚检查了教程,发现如果你从site_layout_test.rb中删除“count:2”,那么测试就会通过。请在你的“/home/ubuntu/workspace/sample_app/test/integration/site_layout_test.rb”中找到它'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=?]", about_path
assert_select "a[href=?]", contact_path
end