虽然按照教程5.3.4布局链接测试失败。
RailsTutorial.org学习导轨5.3.4布局链接测试
这是测试跑:
$ rails generate integration_test site_layout
这是收到的错误:
/usr/local/rvm/gems/ruby-2.2.1/gems/fog-1.23.0/lib/fog/rackspace/mock_data.rb:42: warning: duplicated key at line 80 ignored: "name"
/home/ubuntu/workspace/db/schema.rb doesn't exist yet. Run rake db:migrate to create it, then try again. If you do not intend to use a database, you should instead alter /home/ubuntu/workspace/config/application.rb to limit the frameworks that will be loaded.
开始
FAIL["test_layout_links", SiteLayoutTest, 2015-05-10 20:31:00 +0000]
test_layout_links#SiteLayoutTest (1431289860.34s)
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/1: [====================================================================================================================] 100% Time: 00:00:00, Time: 00:00:00
Finished in 0.53147s
1 tests, 2 assertions, 1 failures, 0 errors, 0 skips
这是文件(测试/集成/site_layout_test.rb)更改d运行测试并收到错误:
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
# test "the truth" do
# assert true
end
end
我在做什么错?
下面是应用程序/视图/布局/ _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>
测试代码看起来不错。错误消息的第二部分是关于测试结果(来自“FAIL”)。它预计有2个到root_path的链接,它确实发现了0. (test/integration/site_layout_test.rb:8表示:在该文件的第nr 8行,这是关于root_path的。) 所以问题不在你的测试文件中,但在其他地方。 看看你的app/views/layouts/_header.html.erb文件。那里有两条根路线,它们是你测试中的count = 2。 – Mauddev
谢谢@Mauddev。我从一开始就一直遇到问题,他们没有工作。文件app/views/layouts/_header.html.erb的代码如下。 –