2012-08-14 69 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]' } 

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

似乎應該有一個更好的解決方案,但至少現在我可以繼續測試。