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

정말 일치 구문을 ... 감사! –