我有一個問題測試下面的模型accossioations:如何測試before_save方法,包括使用RSpec
class Bill < ActiveRecord::Base
belongs_to :consignee
before_save :calc_rate
def calc_rate
self.chargeableweight = self.consignee.destination.rate * self.weight
end
end
收貨人模型:
class Consignee < ActiveRecord::Base
belongs_to :destination
has_many :bills
end
這些控制器尚未觸及。
該應用程序的行爲是正確的(後續問題:該解決方案是否存在任何性能問題?) - 但測試中斷。
你有沒有對象,當你沒有 期待它!您可能預期了Array的一個 實例。在評估零發生錯誤 *
謝謝你的建議, 丹尼
更新:
該法案的測試中斷使用工廠女孩:
describe Bill do
it "should call the calc_rate method" do
bill = Factory.build(:bill)
bill.save!
bill.should_receive(:calc_rate)
end
end
你有一個當你沒有想到它的時候沒有對象!
工廠:
Factory.define :destination do |f|
f.airport_code "JFK"
end
Factory.define :consignee do |f|
...
f.association :destination
end
Factory.define :bill do |f|
f.association :consignee
f.weight 10
f.chargeableweight 20.0
f.after_create do |bill|
bill.calc_rate
end
粘貼您的測試案例... – Jagira 2010-08-08 11:20:28