scala - scala中的数组断言
问题描述
我试图断言条件但出现错误。
我有类型为 Either[CaseClass[Array[String]]] 的结果,其值为:Right(CaseClass(Array("value")))
当我做 :
result should equal(Right(CaseClass(Array("value"))))
它给了我:
Right(CaseClass([Ljava.lang.String;@6ed4e733) did not equal Right(CaseClass([Ljava.lang.String;@43553bf0))
解决方案
Array
不是真正的 Scala 集合并且行为不同,例如
List(42) == List(42) // true
Array(42) == Array(42) // false
我们看到数组在结构上没有比较。现在 ScalaTest 确实提供了特殊处理,Array
确实可以在结构上比较它们
Array("") should equal (Array("")) // pass
Array
但是当嵌套在另一个容器中时它不起作用
case class Foo(a: Array[String])
Foo(Array("")) should equal (Foo(Array(""))) // fail
真正的 Scala 集合,例如List
,不会遇到这个问题
case class Bar(a: List[String])
Bar(List("")) should equal (Bar(List(""))) // pass
There is an open issue Matchers fail to understand Array equality for Arrays wrapped inside a container/collection #491 to address deep equality checks for Array
however for now I would suggest switching to List
instead of Array
. Another options is to provide your own custom equality designed to handle your specific case.
推荐阅读
- mysql - 在加入另一张表时查询以更新一张表
- html - 检查数据值是否已存在于带有液体的 json 文件中
- c++ - 如何在 .dll 中为 Unity 定义预处理器 ifdef?
- python - 在 django 中创建新项目时无法创建进程
- javascript - querySelector 不适用于 puppeteer 中的子元素
- java - 如何在使用 mvn clean install 时避免在不使用 maven 中的 skipTests 的情况下运行测试用例
- laravel - Illuminate\Broadcasting\BroadcastException: auth_key 应该是一个有效的应用程序密钥 laravel 5.8
- javascript - 困扰我一整天的问题。标签中的“type”属性和“as”属性有什么区别?
- python - AES/PKCS5/SHSA256
- javascript - 如何在文本更改上应用新的 CSS