在java.util.Map中“应包含allOf”?

我如何在Java Map上包含一个allOf断言?

以下没有用

val testMap = new java.util.LinkedHashMap[Int, Int]()
testMap.put(1,2)
testMap.put(2,4)
testMap.put(3,6)
testMap should contain allOf (1->2, 2->4, 3->6)

它给

{1=2, 2=4, 3=6} did not contain all of ((1,2), (2,4), (3,6))

医生说

The Aggregating companion object provides implicit instances of Aggregating[L] for types GenTraversable[E], java.util.Collection[E], java.util.Map[K, V], String, Array[E].

http://www.scalatest.org/user_guide/using_matchers#workingWithAggregations

我究竟做错了什么?

Scala     v2.11
ScalaTest v3.1.1
评论
雪

One difference to note between the syntax supported on Java and Scala collections is that in Java, Map is not a subtype of Collection, and does not actually define an element type. You can ask a Java Map for an "entry set" via the entrySet method, which will return the Map's key/value pairs wrapped in a set of java.util.Map.Entry, but a Map is not actually a collection of Entry. To make Java Maps easier to work with, however, ScalaTest matchers allows you to treat a Java Map as a collection of Entry, and defines a convenience implementation of java.util.Map.Entry in org.scalatest.Entry. — http://www.scalatest.org/user_guide/using_matchers#javaCollectionsAndMaps

尝试:

val testMap = new java.util.LinkedHashMap[Int, Int]()
testMap.put(1,2)
testMap.put(2,4)
testMap.put(3,6)
testMap should contain allOf (Entry(1, 2), Entry(2, 4), Entry(3, 6))
点赞
评论