Mozilla 将页脚中的 about 链接渲染为 href="localhost:3000/about",而它应该渲染为 href="/about",但当我使用 chrome 时,不会发生这种情况。
如果能帮助解决我的测试失败的原因,那就太棒了。
测试:
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 #failing
assert_select "a[href=?]", contact_path
end
“捆绑执行 rake 测试”之后:
Expected at least 1 element matching "a[href="/about"]", found 0..
Expected 0 to be >= 1.
相关路线.rb:
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
调用link_to的 View :
<li><%= link_to 'About', about_path %></li>
注意:这是唯一失败的测试,但是当我注释掉该测试时,接触测试就会失败。
编辑:
以下是我的观点:
home.html.erb
<!DOCTYPE html>
<div class="center jumbotron">
<h1>Sample App</h1>
<p>This is the home page for the railtutorial.org book, ch 3</p>
<%= link_to "Sign up now!", signup_path, class: "btn btn-lg btn-primary" %>
</div>
<%= link_to image_tag("rails.png", alt: "Rails logo"), 'http://rubyonrails.org/' %>
</html>
application.html.erb
<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
<%= render 'layouts/shim' %>
</head>
<body>
<%= render 'layouts/header' %>
<div class="container">
<%= yield %>
<%= render 'layouts/footer' %>
</div>
</body>
相关 _footer View 出现错误:
<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>
最佳答案
也许您正在使用about_url
在你看来,而不是 about_path
?
如果没有,则在您的测试中添加此行(在失败行上方):
puts assert_select 'a[href]'
这将打印所有 <a href
标签,您应该能够看到 about 标签,并了解为什么该 URL 不是您期望的那样。
关于ruby-on-rails - Rails 4.2 - 为什么assert_select 失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30198597/