【发布时间】:2015-09-05 16:36:14
【问题描述】:
我正在尝试将 people_controller.rb 文件与 index.erb 文件链接,以便用户可以单击 /people 页面中的名称并通过 people/:id 路由转到唯一页面。这在浏览器中有效,但该应用程序一直未能通过我给出的规范测试。我认为我收到的规范文件不正确,实际上并没有测试链接是否存在。
这是我的 people_controller.rb 文件:
get "/people" do
@people = Person.all
erb :"/people/index"
end
get "/people/:id" do
@person = Person.find(params[:id])
birthdate_string = @person.birthdate.strftime("%m%d%Y")
birth_path_num = Person.get_birth_path_num(birthdate_string)
@message = Person.get_message(birth_path_num)
erb :"/people/show"
end
这是我的 index.erb 文件:
<h1>People</h1>
<table>
<thead>
<th>Name</th>
<th>Birthdate</th>
</thead>
<tbody>
<% @people.each do |person| %>
<tr>
<td>
<a href="<%="people/#{person.id}" %>">
<%= "#{person.first_name} #{person.last_name}" %>
</a>
</td>
<td>
<%= "#{person.birthdate}" %>
</td>
</tr>
<% end %>
</tbody>
</table>
这是我的规范文件:
require 'spec_helper'
describe "Our Person Index Route" do
include SpecHelper
before (:all) do
@person = Person.create(first_name: "Miss", last_name: "Piggy", birthdate: DateTime.now - 40.years )
end
after (:all) do
@person.delete
end
it "displays a link to a person's show page on the index view" do
get("/people")
expect(last_response.body.include?("/people/#{@person.id}")).to be(true)
end
end
这是我尝试使用规范文件运行 rspec 时收到的失败消息:
Failure/Error: expect(last_response.body.include?("/people/#{@person.id}")).to be(true)
expected true
got false
# ./spec/people_show_link_spec.rb:16:in 'block (2 levels) in <top (required)>'
expect 方法实际上是检查链接是否存在,还是只检查人员页面上是否存在文本字符串“/people/#{@person.id}”?如果它实际上正在检查链接,它不应该以某种方式包含“a href”(或其他指示链接的关键字)吗?
【问题讨论】:
标签: ruby activerecord rspec routes sinatra