What is the best way to test a private method calles in before_validation callback

13 views
Skip to first unread message

Javix

unread,
Oct 8, 2012, 4:24:59 PM10/8/12
to rs...@googlegroups.com
I have a private method that doe some calculations in a before_validation callback:

class Operation < ActiveRecord::Base
...#some attributes

before_validation :calculate_interests_and_total

private
 
    def calculate_interests_and_total    
      self.sum ||= 0
      self.rate ||= 0
      self.duration ||= 0
      self.interests = sum * (rate/100 * duration/12)
      self.total = interests + sum   
    end

end

In the oprtation_spec.rb:

describe Operation do
 
  let(:client) { FactoryGirl.create(:client) }
 
  before {
    @operation = client.operations.build(operation_type: Operation::TRANSACTIONS[0], duration: 5, rate: 1.20, sum: 10000)
  }
...
describe "calculate interests and total" do
    before {@operation.save}
    its(:interests) {should eq(@operation.sum * (@operation.rate/100 * @operation.duration/12))}
    its(:total) {should eq(@operation.sum + @operation.interests)}
  end

end

I found the test too verbose and hard coupled with the implementation. Is there a better way to achieve that?
Thank you
Reply all
Reply to author
Forward
0 new messages