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
--
You received this message because you are subscribed to the Google Groups
"rspec" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
To view this discussion on the web visit
https://groups.google.com/d/msg/rspec/-/stsF4N4eMnIJ.
For more options, visit https://groups.google.com/groups/opt_out.