【发布时间】:2017-04-19 16:55:39
【问题描述】:
这是一个玩具示例:
我有一个方法 last[T](ts: Seq[T]): Try[T] 可以返回:
- 以
Success包裹的非空列表的最后一个元素,或 -
NoSuchElementException包裹在Failure中。
我一直在阅读scalatest doc pertaining to TryValues 并想出了以下scalates:
"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 起作用?
【问题讨论】: