學習rails在這裏測試Rails測試:爲什麼關聯引用出錯
我在rails默認測試中出錯。
以下是與此控制器相關的3個錯誤之一。所有錯誤http://pastie.org/9756790
RundataControllerTest#test_should_get_edit:
ActionView::Template::Error: undefined method `name' for nil:NilClass
app/views/rundata/_form.html.erb:15:in `block (2 levels) in _app_views_rundata__form_html_erb___4420969876653961991_47893480'
app/views/rundata/_form.html.erb:15:in `block in _app_views_rundata__form_html_erb___4420969876653961991_47893480'
app/views/rundata/_form.html.erb:1:in `_app_views_rundata__form_html_erb___4420969876653961991_47893480'
app/views/rundata/edit.html.erb:3:in `_app_views_rundata_edit_html_erb__2870890776683796725_47932520'
test/controllers/rundata_controller_test.rb:33:in `block in <class:RundataControllerTest>'
這是在我的測試創建5中的一個錯誤的行。見下面
<%= f.select(:run_id, Run.all.collect {|r| ['Run:' + r.id.to_s + ' for ' + r.company.name + ' to start on ' + r.start_date.strftime("%b %d, %Y"), r.id]}, :selected => params[:run]) -%>
這裏所有的匪徒是我companies.yml燈具
<% 500.times do |c| %>
company_<%= c %>:
name: <%= "company #{c} name" %>
street: <%= Random.new.rand(1000..9999).to_s + ' ' + (0...10).map { ('a'..'z').to_a[rand(26)] }.join + ' street' %>
city: <%= (0...20).map { ('a'..'z').to_a[rand(26)] }.join %>
state: <%= "MN" %>
zip: <%= Random.new.rand(10000..99999) %>
<% end %>
這裏是我的runs.yml燈具
one:
start_date: 2014-09-25
end_date: 2014-09-25
company_id:
building: 1
paid: false
stage: MyString
material_total_lbs: 1
employees_needed: 1
is_test: false
requirements: MyText
two:
start_date: 2014-09-25
end_date: 2014-09-25
company_id:
building: 1
paid: false
stage: MyString
material_total_lbs: 1
employees_needed: 1
is_test: false
requirements: MyText
這裏是我的rundata.yml燈具
one:
run_id:
date: 2014-09-23
building: 1
machine: MyString
material_weight: 1.5
time_mins: 1.5
rate_lb_hr: 1
mill_amps: 1
class_amps: 1
mill_liner: MyString
beater_plate_size: MyString
mill_rpm: 1
class_rpm: 1
feeder_type: MyString
feeder_setting: 1.5
feeder_aug_diameter: 1.5
tlgs_set: 1.5
air_pressure: 1
temp_mill_out: 1
temp_prod_out: 1
temp_ambient: 1
two:
run_id:
date: 2014-09-23
building: 1
machine: MyString
material_weight: 1.5
time_mins: 1.5
rate_lb_hr: 1
mill_amps: 1
class_amps: 1
mill_liner: MyString
beater_plate_size: MyString
mill_rpm: 1
class_rpm: 1
feeder_type: MyString
feeder_setting: 1.5
feeder_aug_diameter: 1.5
tlgs_set: 1.5
air_pressure: 1
temp_mill_out: 1
temp_prod_out: 1
temp_ambient: 1
這些關係都是h as_many - > < -belongs_to
公司 - > < - 運行 - > < - Rundatum
在我看來,這些語句在我的代碼是罪魁禍首。
@company = rundatum.run.company
r.company.name
r.company.name
material.rundatum.id
@material.rundatum.id
Can rails能測試遍歷關聯嗎? 我想這與夾具不包含關係的鍵有關?
任何指導都會很棒。謝謝!
在我的運行夾具'company_id:'對於兩個夾具都是空白的。從我理解的軌道處理這種關聯時,空白。我的公司固件也沒有'id:'我相信汽車產生的條目。 – JerryA 2014-12-02 20:29:34