3

The scala test code:

import play.api.test._
import scala._
import org.specs2.execute.Result

object ThrowTest extends PlaySpecification {

  "throwA" should {
    "catch the exception test1" in {
      world must throwA[Exception]
    }
    "catch the exception test2" in {
      hello {
        world =>
          world must throwA[Exception]
      }
    }
  }

  def hello(action: (String) => Result) = {
    action(world)
  }

  def world: String = {
    throw new Exception("world-exception")
  }

}

Why test1 is working as I expected, but test2 is not, which throws the exception to outer and never catch it:

[info] ! catch the exception test2
[error]     Exception: world-exception (ThrowTest.scala:26)
[error] database.ThrowTest$.world(ThrowTest.scala:26)
[error] database.ThrowTest$.hello(ThrowTest.scala:22)
[error] database.ThrowTest$$anonfun$1$$anonfun$apply$4.apply(ThrowTest.scala:14)
[error] database.ThrowTest$$anonfun$1$$anonfun$apply$4.apply(ThrowTest.scala:14)
[info] Total for specification ThrowTest
4

1 回答 1

2

因为对于测试 2,您的异常是在调用hello 之前action引发的。action是 aString => Result并且你调用它world- 当评估时 - 抛出异常,因此,所有这些代码:

world =>world must throwA[Exception]

永远不会被执行。

于 2014-03-31T12:36:32.813 回答