1
這裏是一個玩具例如:試驗型)scalatest
我有一個方法last[T](ts: Seq[T]): Try[T]
它返回任一:
- 一個非空列表裹着的最後一個元素a
Success
或 - a
NoSuchElementException
包裝在Failure
中。
我一直在閱讀的scalatest doc pertaining to TryValues與以下scalatest上來:
"The solution" should "Find the last element of a non-empty list" in {
last(Seq(1, 1, 2, 3, 5, 8)).success.value should equal (8)
// ...
}
it should "Fail with NoSuchElementException on an empty list" in {
// Option 1: what I would like to do but is not working
last(Nil).failure.exception should be a[NoSuchElementException]
// Option 2: is working but actually throws the Exception, and does not test explicitly test if was in a Failure
a [NoSuchElementException] should be thrownBy {last(Nil).get}
}
有沒有辦法讓我的選項1的工作?
真的不符語法...謝謝! –