jeudi 30 juillet 2015

rspec is not saving to the db a calculated value

I have troubles with one test which I'm writing. I have a Budget model where the promise (column) is been calculated from a Method named calculate_budget after a callback after_create.

In the normal dev mode the application is saving to the db but in the current test I grepping some other budget records where the promise is 0 instead of the calculated promise.

here some code to

before(:each) do
  @member1 = FactoryGirl.create(:member)
  @member2 = FactoryGirl.create(:member, id: "2")
  income = FactoryGirl.create(:income, member: @member1)
  donation1 = FactoryGirl.create(:donation1)
  donation2 = FactoryGirl.create(:donation2)
  @budget = FactoryGirl.create(:budget, donation: donation1, member: @member1)
end


  receipt1 = Receipt.create!(id: 1, date: '2015-01-01', member: @member1)
  receipt2 = Receipt.create!(id: 2, date: '2015-02-01', member: @member1)
  receipt1.items << ReceiptItem.create!(id: 1, donation_id: 1, amount: 10, receipt_id: 1)
  receipt2.items << ReceiptItem.create!(id: 2, donation_id: 1, amount: 20, receipt_id: 2)

  budget2 = FactoryGirl.create(:budget, title: 'budget2', start_date: '2016-01-01', end_date: '2016-12-31', donation_id: 1, member: @member1)

  ap "#budget promise => #{@budget.title} promise #{@budget.promise} remaining #{@budget.remainingPromiseCurrentBudget}"
  ap "#budget promise => #{budget2.title} promise #{budget2.promise} remaining #{budget2.remainingPromiseCurrentBudget}"

  @budget.save
  budget2.save

  ap Budget.all
  # debugger
  ap "budget2.get_all_old_budgets: #{budget2.get_all_old_budgets}"

  expect(budget2.remainingPromiseCurrentBudget).to be(210)

Any idea why the is not saving the record correctly?

Aucun commentaire:

Enregistrer un commentaire