【发布时间】:2010-08-08 11:16:28
【问题描述】:
我在测试以下模型时遇到问题:
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
控制器还没有被触及。
应用程序的行为是正确的(后续问题:该解决方案是否存在任何性能问题?) - 但测试中断。
当你没有时你有一个 nil 对象 期待它!你可能已经预料到了 数组的实例。发生错误 在评估 nil 时。*
谢谢你的建议, 丹尼
更新:
这个账单测试打破了工厂女孩:
describe Bill do
it "should call the calc_rate method" do
bill = Factory.build(:bill)
bill.save!
bill.should_receive(:calc_rate)
end
end
你没想到的是一个 nil 对象!
工厂:
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
【问题讨论】:
-
粘贴你的测试用例...
标签: ruby-on-rails ruby unit-testing rspec