2017-04-19 24 views
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的工作?

回答

3

您应该使用shouldBe字断言类型,如:

test.failure.exception shouldBe a [NoSuchElementException] 

类型不相等,比如:

test.failure.exception should not be an [NoSuchElementException] 

查看更多: http://www.scalatest.org/user_guide/using_matchers

+0

真的不符语法...谢谢! –