views:

205

answers:

2

Is it possible to use RSpec .should(change(...)).by(...) with float numbers and set the compare precision like this:

lambda { ...}.should change(unit, :price).by(12.151, 10e-5)

Thanks,

A: 

It does not seem to be possible with the current state of the code. Here is the source of the Matchers::Change, where you can see it: http://github.com/dchelimsky/rspec/blob/f9ef5bff62c10d33414c9ab2f3ac87e1256e07b8/lib/spec/matchers/change.rb

Would it be acceptable to match a upper bound? For example:

lambda { ...}.should change(unit, :price).by_at_most(12.15)
pschneider
Yes, that could solve the problem if we use by_at_most and by_at_least together. Not a perfect solution but works.
Bogdan Gusiev
A: 

We can always write the custom matcher.

Or use the combination of by_at_most and by_at_least matchers with the precision like this:

lambda { ...}.should change(unit, :price).by_at_most(12.15 + 10e-5).by_at_least(12.15 - 10e-5)
Bogdan Gusiev