Ruby Rspec';应该改变';带浮点数

Ruby Rspec';应该改变';带浮点数,ruby,rspec,floating-point,Ruby,Rspec,Floating Point,是否可以使用带浮点数的RSpec.should(change(…).by(…),并按如下方式设置比较精度: lambda { ...}.should change(unit, :price).by(12.151, 10e-5) lambda { ...}.should change(unit, :price).by_at_most(12.15 + 10e-5).by_at_least(12.15 - 10e-5) 谢谢,以代码的当前状态,这似乎是不可能的。以下是Matchers::Chang

是否可以使用带浮点数的RSpec
.should(change(…).by(…)
,并按如下方式设置比较精度:

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

谢谢,

以代码的当前状态,这似乎是不可能的。以下是Matchers::Change的源代码,您可以在其中看到它:

匹配上限是否可以接受?例如:

lambda { ...}.should change(unit, :price).by_at_most(12.15)

我们总是可以编写自定义匹配器

或者使用“最多按”匹配器和“最少按”匹配器的组合,精度如下:

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

是的,如果我们最多使用by_,至少一起使用by_,这可以解决问题。这不是一个完美的解决方案,但很有效。