Javix
unread,Oct 8, 2012, 4:24:59 PM10/8/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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