简体   繁体   English

从specs2 helper方法返回类型

[英]Return type from specs2 helper method

Let's assume I write a helper method, that accepts some test code. 我们假设我编写了一个辅助方法,它接受一些测试代码。

def testWithPrintln(test: => A):A = {
    println("I'm happy and testing")
    test
}

What should be the A type be? 什么应该是A型? What is the right one? 什么是正确的? I'm browsing the specs2 api and there are numerous similar looking types: AsResult[_] , Result , MatchResult[_] , I'm confused what to use. 我正在浏览specs2 api并且有许多类似的外观类型: AsResult[_]ResultMatchResult[_] ,我很困惑使用什么。

I think what you are looking for is org.specs2.execute.Result . 我认为你要找的是org.specs2.execute.Result I believe this quick example demonstrates what you were going for: 我相信这个简单的例子展示了你的目标:

import org.specs2.mutable.Specification
import org.specs2.execute.Result

class PrintTest extends Specification{

  "A request to do something" should{
    "succeed like this" in testWithPrintln{
      "foo" mustEqual "foo"            
    }
    "also succeed like this" in {
      testWithPrintln{
        "foo" mustEqual "foo"            
      }
    }
  }

  def testWithPrintln(test: => Result) {
      println("I'm happy and testing")
      test
  }  
}

I believe that the AsResult type is used in implicit conversions between basic types (like Boolean) that are not themselves Result s but can be converted to a Result . 我相信AsResult类型用于基本类型(如Boolean)之间的隐式转换,它们本身不是Result但可以转换为Result If you look at the javadoc comment for AsResult it states: 如果你看一下AsResult的javadoc评论,它说明:

Typeclass trait for anything that can be transformed to a Result

As for MatchResult , I believe this is used when performing matching on things like params when using stubbing via Mockito. 至于MatchResult ,我相信这是在使用Mockito使用存根时对params这样的事情进行匹配时使用的。 I've used MatchResult a few times explicitly when defining custom matchers for params in my mock stubbing. 在我的模拟存根中为params定义自定义匹配器时,我已经使用过MatchResult几次。

Trying to elaborate on @cmbaxter's answer. 试着详细说明@cmbaxter的答案。

In specs2 the body of an Example needs to be evaluated as a Result , that is either a Success or Failure or Error or Skipped or Pending . 在specs2中, Example的主体需要作为Result进行评估,即SuccessFailureErrorSkippedPending In order to provide enough flexibility, the body of an Example will accept any type T that can be transformed to a Result provided that an instance of AsResult[T] is in (the implicit) scope. 为了提供足够的灵活性, Example的主体将接受任何可以转换为Result类型T ,前提是AsResult[T]的实例在(隐式)范围内。

There are instances of AsResult for various types: 有各种类型的AsResult实例:

  • Boolean : this makes true being a Success and false being a failure Boolean :这使得true成为Successfalse成为failure

  • Result itself, just returning the value Result本身,只返回值

  • MatchResult[T] : a MatchResult is the result of a matcher execution. MatchResult[T]MatchResult是匹配器执行的结果。 This is the result of expressions such as 1 must beEqualTo(1) 这是表达式的结果,例如1 must beEqualTo(1)

  • org.scalacheck.Prop to execute a ScalaCheck property in an example org.scalacheck.Prop在一个例子中执行ScalaCheck属性

In your example, the test helper will work fine if you implement it like this: 在您的示例中,如果您实现它,测试助手将正常工作:

// if `a: A` is acceptable as the body of an example
// you can use testWithPrintln(a) in your example
def testWithPrintln[A : AsResult](a: A): A = {
  println(a)
  a
} 

Note however that the helper you are looking for might already exist in specs2. 但请注意,您要查找的帮助程序可能已存在于specs2中。 You can use the .pp method to "print and pass" any value: 您可以使用.pp方法“打印并传递”任何值:

// has type MatchResult[Int]
(1 must_== 1).pp

Also you can add more verbose messages to your expectations: 您还可以根据自己的期望添加更详细的消息:

 // will print "this is not correct because 1 is not equal to 2"
 "this is correct" ==> { 1 must_== 2 }

 // will print "the number of elements: 1 is not greater than 2
 val n = 1
 n aka "the number of elements" must be_>(2)

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

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