我对其中一个练习中的测试有一个非常具体的问题。我已经通过所有不同的文件追溯到失败,无法看到导致问题的原因。我将从失败消息开始。
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)
这里是与用户的夹具文件:迈克尔的名字&#34;迈克尔的例子&#34;出现两次但应该只出现一次。
测试/装置/ users.yml里
michael:
name: Michael Example
email: michael@example.com
password_digest: <%= User.digest('password') %>
admin: true
activated: true
activated_at: <%= Time.zone.now %>
.
.
.
这是该夹具用于创建标题的代码
应用程序/助手/ 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
为什么当代码声明在页面标题中只出现一次时,用户夹具的名称会出现两次?
编辑:
应用程序/视图/ 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>
答案 0 :(得分:0)
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>````