2017-04-19 64 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

真的不符語法...謝謝! –