I have a failing rspec view test but the code works – I probably have a variable incorrectly setup but can’t figure out what it is.
When I display the contents of @incident_report (pp @incident_report) in my spec, it properly displays the record created by FactoryGirl.
When I display the actual rendered content (puts rendered), it shows the values from the the record I created with FactoryGirl…
But the “rendered.should contain(work_order)” spec fails with:
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":
and none of the data is displayed, only the HTML template
spec/views/incident_report/show.html.haml_spec.rb code
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 code
%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}
Gotta be something really simple I’m overlooking.
Turns out it’s because I was using simple_form and when simple_form displays for a “show” action, it puts the field values into the html as a ‘value=”54785″‘ attribute. If you display it in a browser, the labels and values all show up correctly, but rspec can’t see them.
I had to add
to my example to get it to work.
Seems like there should be a better solution but at least now I can continue testing.