本文介绍了在 ScalaTest 中处理双打的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我刚刚开始使用 ScalaTest,我正在使用以下内容来比较我的规范中的两个双打,如下所示:

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 {
    val x = new X(10,10,12,1000)
    assert(x.price() === 185.92)
  }

即使我输入了错误的 185.92 值来与价格函数返回的值进行比较(对于上述情况,实际上返回 10.23),规范仍在通过.我有其他规范,我只是比较 Ints 并且它们按预期工作.但是涉及 Double 的那些都通过了.除了 assert 我还应该使用其他函数来比较 Double 吗?

The spec is passing even though I have put in a wrong value of 185.92 to compare against what the price function is returning (that actually returns 10.23 for the case above). I have other specs where I just compare Ints and they work as expected. But the ones involving Doubles are passing regardless. Is there another functions besides assert I should be using to compare Doubles?

def price () : Double

推荐答案

在我看来,你在 行的范围内有一个 Equality[Double] 的隐式实例org.scalactic.TolerantNumerics,其文档为 这里.

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.

文档中的示例是:

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)

这篇关于在 ScalaTest 中处理双打的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 21:51