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的工作?
真的不符语法...谢谢! –