0
下面的方法適當地通過測試:請說明爲什麼下面的測試失敗
def associate_grid_location
devices.each do |device|
if device.grid_location and grid_location_id.nil?
update_attribute(:grid_location_name,device.grid_location.grid_name)
update_attribute(:grid_location_id,device.grid_location_id)
break
end
end
end
但是,如果我運行這個方法的調用相同的測試邏輯,它沒有更新每個櫃grid_location_name和grid_location_id :
def self.associate_grid_locations
Cabinet.all.each do |cabinet|
cabinet.associate_grid_location
end
end
請提出爲什麼它可能會失敗。
通過測試:
test "Cabinet associate grid location from device where device has grid location" do
@cabinet = cabinets(:one)
@cabinet.grid_location_id = nil
@device = devices(:one)
@grid_location = grid_locations(:one)
@grid_location.grid_name = "test location"
@device.grid_location = @grid_location
@cabinet.devices << @device
@cabinet.associate_grid_location
assert_equal @grid_location.grid_name, @cabinet.grid_location_name, "Failed: Did not update cabinet grid location"
assert_equal @device.grid_location.id, @cabinet.grid_location_id, "Failed: Did not update cabinet grid location"
end
失敗的測試:
test "Cabinet associate grid location from device where device has grid location (Called on Cabinet)" do
@cabinet = cabinets(:one)
@cabinet.grid_location_id = nil
@device = devices(:one)
@grid_location = grid_locations(:one)
@grid_location.grid_name = "test location"
@device.grid_location = @grid_location
@cabinet.devices << @device
Cabinet.associate_grid_locations
assert_equal @grid_location.grid_name, @cabinet.grid_location_name, "Failed: Did not update cabinet grid location"
assert_equal @device.grid_location.id, @cabinet.grid_location_id, "Failed: Did not update cabinet grid location"
end
謝謝。
你可以請進一步解釋一下嗎?我在哪裏打電話重新加載?在致電Cabinet.associate_grid_locations ''之前? – 2013-05-14 14:49:11
對'Cabinet.associate_grid_locations'的調用會更新數據庫,因此您希望在調用**之後調用reload **,以使測試中的對象填充更新後的數據。 – 2013-05-14 19:50:59