2017-04-19 3 views
1

玩具例である:私は返すメソッドlast[T](ts: Seq[T]): Try[T]を有するテストタイプ)ここscalatestと

いずれか:

  • に包まれた非空リストの最後の要素Successまたは
  • NoSuchElementExceptionFailureにラップします。

私は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

本当に一貫性のない構文を...ありがとう! –

関連する問題