Handling Doubles in ScalaTest

前端 未结 2 786
旧时难觅i
旧时难觅i 2020-12-18 22:59

I have just started using ScalaTest and I am using the following to compare two Doubles in my spec as follows:

  it should \"calculate the price\" in {
    v         


        
相关标签:
2条回答
  • 2020-12-18 23:22

    You can simply do actual shouldBe (expected +- tolerance) if using FlatSpec; other specs have similar matchers instead of shouldBe. It gives better messages in case of failures than assert, and the code is consistent with the other tests.

    0 讨论(0)
  • 2020-12-18 23:29

    It looks to me like you've got an implicit instance of Equality[Double] in scope along the lines of org.scalactic.TolerantNumerics, for which the documentation is here.

    The example from the doc is:

    implicit val doubleEquality = TolerantNumerics.tolerantDoubleEquality(0.01)
    

    But it looks like somebody has instantiated it with a really big tolerance value in your case.

    You may also consider trying explicit tolerance by using +-:

    assert(x.price() === 185.92 +- 0.01)
    
    0 讨论(0)
提交回复
热议问题