# Testing BigDecimal and sum

**URL:** <https://discuss.rubyonrails.org/t/testing-bigdecimal-and-sum/56455>\
**Category:** rubyonrails-talk\
**Created:** [December 26, 2010, 4:37pm UTC](https://discuss.rubyonrails.org/t/testing-bigdecimal-and-sum/56455 "2010-12-26T16:37:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daryn](https://avatars.discourse-cdn.com/v4/letter/d/a87d85/32.png) [@Daryn](https://discuss.rubyonrails.org/u/Daryn)\
**Post date:** [December 26, 2010, 4:37pm UTC](https://discuss.rubyonrails.org/t/testing-bigdecimal-and-sum/56455/1 "2010-12-26T16:37:41Z")

</div>

Hi

I have the following test:

it "calculates the actual expenditure per month" do &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@expense = Expense.new(:name =\> "rent", :limit =\> 100.50) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@expense.save &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@expense.transactions \<\< Transaction.new(:t\_date =\> Date.civil(2010,01,05), :description =\> "Payment for rent", :value =\> 300) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@expense.transactions \<\< Transaction.new(:t\_date =\> Date.civil(2010,01,10), :description =\> "Payment for rent", :value =\> 200) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@expense.transactions \<\< Transaction.new(:t\_date =\> Date.civil(2010,02,01), :description =\> "Payment for rent", :value =\> 100) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@expense.actual.should == BigDecimal('600') end

The actual method in expense looks like this: &nbsp;&nbsp;def actual &nbsp;&nbsp;&nbsp;&nbsp;transactions.sum('value') &nbsp;&nbsp;end

I have noticed that I have to save @expense for this to work, why is that? Are the transactions saved when I assign them to the saved @expense using \<\< ? Is there a better way to write this test, perhaps without hitting the database?

My second question is with regards to this line: @expense.actual.should == BigDecimal('600')

Is that a common way of testing BigDecimal? What is considered best practices when dealing with this?

Thanks, Daryn

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [December 26, 2010, 7:32pm UTC](https://discuss.rubyonrails.org/t/testing-bigdecimal-and-sum/56455/2 "2010-12-26T19:32:31Z")

</div>

> Hi
> 
> I have the following test:
> 
> it "calculates the actual expenditure per month" do @expense = Expense.new(:name =\> "rent", :limit =\> 100.50) @expense.save@expense.transactions \<\< Transaction.new(:t\_date =\> Date.civil(2010,01,05), :description =\> "Payment for rent", :value =\> 300) @expense.transactions \<\< Transaction.new(:t\_date =\> Date.civil(2010,01,10), :description =\> "Payment for rent", :value =\> 200) @expense.transactions \<\< Transaction.new(:t\_date =\> Date.civil(2010,02,01), :description =\> "Payment for rent", :value =\> 100) @expense.actual.should == BigDecimal('600') end
> 
> The actual method in expense looks like this: def actual transactions.sum('value') end
> 
> I have noticed that I have to save @expense for this to work, why is that?

because the sum method on an association always does "Select sum(column\_name) from ..." so if the objects are unsaved then nothing is found (Active record's sum method hides the ruby sum method). If the expense object isn't saved then the transactions can't be saved since expense\_id is not known)

Fred
