如何将 ShouldVerb 与 Double 类型一起使用?
How to use ShouldVerb with Double types?
我不熟悉使用完全成熟的 scalatest 和 3.0.0 版中相对较新的 ShouldVerb
(例如 ShouldMatchers
)。我想测试的是以下内容:
import org.scalatest.FunSuite
import org.scalatest.words.ShouldVerb
class MyFunTest extends FunTest with ShouldVerb {
test("the world makes sense") {
math.abs(1.0 - (1.0 - 1e-10)) should be < 1e-9
}
}
但这会导致编译器错误:
value should is not a member of Double
文档指出 ShouldVerb“提供隐式转换,将 should 方法添加到 String 以支持 FlatSpec、WordSpec、fixture.FlatSpec 和 fixture.WordSpec."
因此,Double
不会隐式修饰 should 动词(仅 String
)。
如果你想使用动词样式,下面的替代(但可以说更冗长)断言版本。
import org.scalatest.{ FlatSpecLike, Matchers }
class MathTests extends FlatSpecLike with Matchers {
"Behavior of math.abs" should "return an absolute number blah blah blah..." in {
assert(math.abs(1.0 - (1.0 - 1e-10)) < 1e-9)
}
}
我不熟悉使用完全成熟的 scalatest 和 3.0.0 版中相对较新的 ShouldVerb
(例如 ShouldMatchers
)。我想测试的是以下内容:
import org.scalatest.FunSuite
import org.scalatest.words.ShouldVerb
class MyFunTest extends FunTest with ShouldVerb {
test("the world makes sense") {
math.abs(1.0 - (1.0 - 1e-10)) should be < 1e-9
}
}
但这会导致编译器错误:
value should is not a member of Double
文档指出 ShouldVerb“提供隐式转换,将 should 方法添加到 String 以支持 FlatSpec、WordSpec、fixture.FlatSpec 和 fixture.WordSpec."
因此,Double
不会隐式修饰 should 动词(仅 String
)。
如果你想使用动词样式,下面的替代(但可以说更冗长)断言版本。
import org.scalatest.{ FlatSpecLike, Matchers }
class MathTests extends FlatSpecLike with Matchers {
"Behavior of math.abs" should "return an absolute number blah blah blah..." in {
assert(math.abs(1.0 - (1.0 - 1e-10)) < 1e-9)
}
}