2012-08-14 72 views
0

我有一个失败的rspec视图测试,但代码工作 - 我可能有一个变量设置错误,但无法弄清楚它是什么。rspec呈现视图未包含可变数据

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

当我显示实际呈现的内容(放置渲染),它显示了我FactoryGirl创建记录的值...

但“rendered.should包含(w​​ork_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模板


规格/视图/ 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} 

得是很简单的东西我俯瞰。

+0

这是一段时间以前,所以我猜你有这个排序,但我会检查一些事情只是为了确保work_order变量具有您期望的值'(work_order.should == “无所谓”)'。此外,如果你的work_order不包含字符串,我不知道包含的是多么聪明,可能值得做一些像'rendered.should包含(w​​ork_order.to_s) – 2012-08-14 15:01:43

回答

0

原来,这是因为我使用的是simple_form,当simple_form为“show”操作显示时,它将字段值作为“value =”54785“'属性放入html中。如果您在浏览器中显示它,标签和值都会正确显示,但rspec无法看到它们。

我不得不添加

rendered.should have_tag 'input', :with => { :value => "54765", :name => 'incident_report[work_order]' } 

我的例子来得到它的工作。

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