Scala 中的 shouldBe 与 shouldEqual 有什么区别? [英] What's the difference between shouldBe vs shouldEqual in Scala?

查看:67
本文介绍了Scala 中的 shouldBe 与 shouldEqual 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么时候应该使用shouldBe,什么时候应该使用shouldEqual?

When should I be using shouldBe and when should I be using shouldEqual?

port shouldEqual 8000
port shouldBe 8000

推荐答案

来自 http://www.scalatest.org/user_guide/using_matchers#checkingEqualityWithMatchers:

result shouldEqual 3//可以自定义相等,不需要括号

result shouldEqual 3 // can customize equality, no parentheses required

result shouldBe 3//不能自定义相等,所以编译速度最快,不需要括号

result shouldBe 3 // cannot customize equality, so fastest to compile, no parentheses required

第一个采用隐式 Equality[T] 来验证计算值与预期值,第二个没有.所以如果你只是想比较端口号shouldBe就足够了.

The first one takes an implicit Equality[T] to verify the computed value with the expected value, the second one doesn't. So if you just want to compare the port number shouldBe is sufficient.

这篇关于Scala 中的 shouldBe 与 shouldEqual 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆