1

我有一个名为 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

错误消息:“参数数量错误(0 代表 3)”

但是,当我执行 a 时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 
4

1 回答 1

0
class Booking < ActiveRecord::Base
validates :to, :from, :amount, presence: true

  def totalamount
    total = (amount + deposit + fee)
    return total 
  end 
end

只需在“totalamount”之后删除 3 个必需的属性。

于 2014-08-23T00:36:58.290 回答