【发布时间】:2012-08-14 04:44:06
【问题描述】:
我的 rspec 视图测试失败,但代码有效 - 我可能有一个变量设置不正确,但无法弄清楚它是什么。
当我在我的规范中显示@incident_report (pp @incident_report) 的内容时,它会正确显示 FactoryGirl 创建的记录。
当我显示实际渲染的内容(渲染的放置)时,它会显示我使用 FactoryGirl 创建的记录中的值...
但是“rendered.should contains(work_order)”规范失败了:
1) incident_reports/show.html displays the work order number on the incident
Failure/Error: rendered.should contain(work_order)
expected the following element's content to include "54785":
不显示任何数据,只显示 HTML 模板
spec/views/incident_report/show.html.haml_spec.rb 代码
require 'spec_helper'
describe "incident_reports/show.html" do
before(:each) do
@incident_report = Factory(:incident_report)
end
it "displays the work order number on the incident" do
work_order = @incident_report.work_order
pp @incident_report #displays an incident_report, id => 1
assign(:incident_report, @incident_report)
render
puts rendered #this DOES have the content from @incident_report
rendered.should contain("Work Order:")
rendered.should contain(work_order)
end
end
show.html.haml 代码
%h1 Display Incident Report
.navigation
= link_to 'Edit', edit_incident_report_path(@incident_report)
|
\#{link_to 'Back', incident_reports_path}
= render 'form'
.navigation
= link_to 'Edit', edit_incident_report_path(@incident_report)
|
\#{link_to 'Back', incident_reports_path}
必须是一些我忽略的非常简单的东西。
【问题讨论】:
-
这是不久前的事了,所以我猜你已经整理好了,但我要检查几件事以确保 work_order 变量具有你所期望的值 @987654324 @。此外,如果您的 work_order 不包含字符串,我不确定 contains 有多聪明,可能值得做类似
rendered.should contain(work_order.to_s)的事情
标签: ruby-on-rails-3 rspec views