简体   繁体   English

一些列表spec2匹配器

[英]Some List specs2 matcher

Why doesn't the following work? 为什么以下工作不起作用?

Some(List()) must beSome(Nil)

'Some(List())' is Some but List() is not equal to 'org.specs2.matcher.ValueChecks$$anon$3@480ba116' 'Some(List())'是Some但List()不等于'org.specs2.matcher.ValueChecks$$anon$3@480ba116'

Note that 注意

Some(List()) must beSome(List())

also don't work. 也行不通。

 'Some(List())' is Some but List() is not equal to 
 'org.specs2.matcher.ValueChecks$$anon$4@48d1978f' 
 Actual:   List()
 Expected: org.specs2.matcher.ValueChecks$$anon$4@48d1978f

So the problem is not Nil 所以问题不是Nil

We know that what we are really doing is something like: 我们知道我们真正在做的事情是这样的:

 Some(List()).must(beSome(List()))

The problem seems with beSome . 问题似乎与beSome Let's see what beSome is returning: 让我们看看beSome返回了什么:

val bl = beSome(List())  // SomeCheckedMatcher[Int]
val bn = beSome(Nil)     // SomeCheckedMatcher[Int]

This doesn't seen right since the return type is like were are checking an Option for a Integer: 由于返回类型就像正在检查Option的Integer一样,所以这种情况并不正确:

val b = beSome(2)        // SomeCheckedMatcher[Int]

And those are not our target types: 这些不是我们的目标类型:

val myList = List()  // myList: List[Nothing] = List()
val myList2 = Nil    // myList2: scala.collection.immutable.Nil.type = List()

So, what's wrong? 那怎么了

Looking at the documentation (Option/Either) , you can use beSome the folowing ways: 查看文档(Option / Either) ,可以使用beSome几种方式:

  • beSome check if an element is Some(_) beSome检查元素是否为Some(_)

  • beSome(exp) check if an element is Some(exp) beSome(exp)检查元素是否为Some(exp)

  • beSome(matcher) check if an element is Some(a) where a satisfies the matcher beSome(matcher)检查元素是否为满足匹配器的Some(a)

  • beSome(function: A => AsResult[B]) check if an element is Some(a) where function(a) returns a successful Result (note that a Seq[A] is also a function Int => A so if you want to check that a sequence is contained in Some you need to use a matcher: beSome(===(Seq(1))) beSome(function: A => AsResult[B])检查元素是否为Some(a) ,其中function(a)返回成功的Result (请注意, Seq[A]也是函数Int => A因此如果需要检查某项中是否包含一个序列,您需要使用一个匹配器: beSome(===(Seq(1)))

The last alternative seems to be our problem here. 最后一个选择似乎是我们这里的问题。 Note the List() is like Seq , a function from Int => A. In, our example: 注意, List()就像Seq一样,是Int => A中的一个函数。在我们的示例中:

val myList = List()                 // myList: List[Nothing] = List()
val func: Int => Nothing = myList   // func: Int => Nothing = List()

To fix this we should use a matcher (the third alternative of the documentation): 为了解决这个问题,我们应该使用匹配器(文档的第三种替代方法):

Some(List()) must beSome(beEqualTo(List()))
Some(List()) must beSome(beEqualTo(Nil))

// or 

Some(List()) must beSome(be_==(List()))  
Some(List()) must beSome(be_==(Nil))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM