我对其中一项练习中的测试有一个非常具体的问题。我已经通过所有不同的文件追溯了故障,但是看不到是什么引起了问题。我将从失败消息开始。
FAIL["test_profile_display", UsersProfileTest, 1.204680988099426]
test_profile_display#UsersProfileTest (1.20s)
<Michael Example | Ruby on Rails Tutorial Sample App> expected but was
<Michael ExampleMichael Example | Ruby on Rails Tutorial Sample App>..
Expected 0 to be >= 1.
test/integration/users_profile_test.rb:13:in `block in <class:UsersProfileTest>'
可以看出,我的灯具文件中的名称出现了两次,而不是一次。
和产生失败的相应测试:测试/集成/users_profile_test.rb:
require 'test_helper'
class UsersProfileTest < ActionDispatch::IntegrationTest
include ApplicationHelper
def setup
@user = users(:michael)
end
test "profile display" do
get user_path(@user)
assert_template 'users/show'
assert_select 'title', full_title(@user.name)
assert_select 'h1', text: @user.name
assert_select 'h1>img.gravatar'
assert_match @user.microposts.count.to_s, response.body
assert_select 'div.pagination'
@user.microposts.paginate(page: 1).each do |micropost|
assert_match micropost.content, response.body
end
end
end
上面代码的冒犯行是
assert_select 'title', full_title(@user.name)
这里是用户名称为“ Michael Example”的用户:michael的Fixtures文件,该文件出现了两次,但只能出现一次。
test / fixtures / users.yml
michael:
name: Michael Example
email: [email protected]
password_digest: <%= User.digest('password') %>
admin: true
activated: true
activated_at: <%= Time.zone.now %>
.
.
.
这里是此灯具用于创建标题的代码
app / helpers / application_helper.rb
module ApplicationHelper
# Returns the full title on a per-page basis.
def full_title(page_title = '')
base_title = "Ruby on Rails Tutorial Sample App"
if page_title.empty?
base_title
else
page_title + " | " + base_title
end
end
end
为什么在代码说明中,用户固定装置的名称在页面标题中只出现一次,为什么会出现两次?
编辑:
app / views / layouts.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">
<% flash.each do |message_type, message| %>
<%= content_tag(:div, message, class: "alert alert-#{message_type}") %>
<!--<div class="alert alert-<%= message_type %>"><%= message %></div>-->
<% end %>
<%= yield %>
<%= render 'layouts/footer' %>
<%= debug(params) if Rails.env.development? %>
</div>
</body>
编辑2:users / show.html.erb
<% provide(:title, @user.name) %>
<div class="row">
<aside class="col-md-4">
<section class="user_info">
<h1>
<%= gravatar_for @user %>
<%= @user.name %>
</h1>
</section>
</aside>
<div class="col-md-8">
<% if @user.microposts.any? %>
<h3>Microposts (<%= @user.microposts.count %>)</h3>
<ol class="microposts">
<%= render @microposts %>
</ol>
<%= will_paginate @microposts %>
<% end %>
</div>
</div>
app / views / users / show.html.erb
<div class="row">
<aside class="col-md-4">
<section class="user_info">
<h1>
<%= gravatar_for @user %>
<%= @user.name %>
</h1>
</section>
</aside>
<div class="col-md-8">
<% if @user.microposts.any? %>
<h3>Microposts (<%= @user.microposts.count %>)</h3>
<ol class="microposts">
<%= render @microposts %>
</ol>
<%= will_paginate @microposts %>
<% end %>
</div>
</div>````