Ruby on rails 3 rspec呈现视图无法包含变量数据

Ruby on rails 3 rspec呈现视图无法包含变量数据,ruby-on-rails-3,rspec,views,Ruby On Rails 3,Rspec,Views,我有一个失败的rspec视图测试,但代码工作-我可能有一个变量设置不正确,但无法找出它是什么 当我在规范中显示@incident_report(pp@incident_report)的内容时,它会正确显示FactoryGirl创建的记录 当我显示实际渲染内容(puts rendered)时,它会显示我用FactoryGirl创建的记录中的值 但是“rendered.should contain(工单)”规范失败,原因是: 1) incident_reports/show.html display

我有一个失败的rspec视图测试,但代码工作-我可能有一个变量设置不正确,但无法找出它是什么

当我在规范中显示@incident_report(pp@incident_report)的内容时,它会正确显示FactoryGirl创建的记录

当我显示实际渲染内容(puts rendered)时,它会显示我用FactoryGirl创建的记录中的值

但是“rendered.should contain(工单)”规范失败,原因是:

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}

我忽略了一些非常简单的东西。

事实证明,这是因为我使用的是简单表单,当简单表单显示“显示”动作时,它会将字段值作为“value=”54785“属性放入html中。如果在浏览器中显示,则标签和值都会正确显示,但rspec无法看到它们

我必须补充一点

rendered.should have_tag 'input', :with => { :value => "54765", :name => 'incident_report[work_order]' }
以我的例子让它发挥作用


似乎应该有更好的解决方案,但至少现在我可以继续测试了。

这是很久以前的事了,所以我猜你已经把它整理好了,但是我要检查几件事,以确保work_order变量具有你期望的值
(work_order.should==“whatever”)
。另外,如果您的工单不包含字符串,我不确定contains有多聪明,可能值得做一些类似于
rendered.should contain(工单.to)