我有一个名为Booking的模型,应该从几个数字计算总数(金额,存款和费用都加在一起)。我无法在Faker中看到这些论点。
it "should calculate the total" do
myvar = FactoryGirl.create(:booking, :amount => 900, :deposit => 20, :fee => 8)
myvar.totalamount.should == 928
end
这是我的方法:
class Booking < ActiveRecord::Base
validates :to, :from, :amount, presence: true
def totalamount(amount,deposit,fee)
total = (amount + deposit + fee)
return total
end
end
错误消息:&#34;参数数量错误(0表示3)&#34;
但是,当我执行puts myvar.deposit
时,它会返回我给它的值 - 20。
我究竟做错了什么?
编辑:这是我的预订工厂版本:
FactoryGirl.define do
factory :booking do |b|
b.from { Faker::Lorem.sentence(word_count=3) }
b.to { Faker::Lorem.sentence(word_count=3) }
b.amount { Faker::Number.digit }
end
end
答案 0 :(得分:0)
class Booking < ActiveRecord::Base
validates :to, :from, :amount, presence: true
def totalamount
total = (amount + deposit + fee)
return total
end
end
只需在'totalamount'之后删除3个必需属性。