RSPEC - 测试说对象是零,但在实践中工作正常

时间:2013-11-25 01:08:02

标签: ruby-on-rails testing rspec

我无法弄清楚为什么这个物体不断出现为零。

这是错误:

  1) Item Calculate with just Total
     Failure/Error: subject.calculate_tax(tax, sub_category)
     TypeError:
       nil can't be coerced into Fixnum
     # ./app/models/item.rb:111:in `+'
     # ./app/models/item.rb:111:in `calculate_tax'
     # ./spec/models/item_spec.rb:26:in `block (2 levels) in <top (required)>'

这是它适用的行 - 它认为“self.tax_rate”是nill(倒数第二个参数)

self.tax_amount     = ((self.total - self.deduction) - ((self.total - self.deduction) / (1 + self.tax_rate))) * self.tax_adjustment

这是我的测试

describe Item do
  subject {Item.new(:report_id => 26 ,  :name => 'Gas' ,:tax_rate => 0.13, :tax_id => 1 ,  :category_id => 15 ,  :sub_category_id => 31 ,  :job_id => 1 , :total => 20 )}

  let(:tax) {Tax.where(id: subject.tax_id).first}
  let(:sub_category) {SubCategory.where(id: subject.sub_category_id).first}


  it 'Calculate with just Total' do

    subject.name.should be == 'Gas'
    tax = Tax.find_by_id(subject.tax_id)

    subject.sub_category_id.should be == 31
    subject.set_nil_values
    sub_category.should_receive(:taxable).exactly(3).times.and_return(sub_category.taxable)
    tax.should_receive(:rate).exactly(4).times.and_return(tax.rate)
    sub_category.should_receive(:tax_adjustment).and_return(sub_category.tax_adjustment)
    subject.calculate_tax(tax, sub_category)
    subject.should_receive(:tax_rate).exactly(2).times.and_return(tax.rate)
    subject.calculate_cost
    subject.cost.should be_within(0.01).of(17.70)
    subject.tax_amount.should be_within(0.01).of(2.30)
    subject.save
  end

1 个答案:

答案 0 :(得分:1)

您的测试数据库中的taxes表似乎没有id等于1且tax.rate不是nil

的条目