3

スカラ テスト コード:

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")
  }

}

test1期待どおりに機能しているのに、test2そうではなく、outer に例外をスローし、それをキャッチしないのはなぜですか。

[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 に答える