Question

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? 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.

Was it helpful?

Solution 2

Trying to elaborate on @cmbaxter's answer.

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. 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.

There are instances of AsResult for various types:

  • Boolean: this makes true being a Success and false being a failure

  • Result itself, just returning the value

  • MatchResult[T]: a MatchResult is the result of a matcher execution. This is the result of expressions such as 1 must beEqualTo(1)

  • org.scalacheck.Prop to execute a ScalaCheck property in an example

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. You can use the .pp method to "print and pass" any value:

// 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)

OTHER TIPS

I think what you are looking for is 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 Results but can be converted to a Result. If you look at the javadoc comment for AsResult it states:

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. I've used MatchResult a few times explicitly when defining custom matchers for params in my mock stubbing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top