2017-01-19 8 views
3

私はマッチャーを使用して、テスト中に例外タイプを主張しようとしていた中で、インスタンスのタイプと一致する方法(理由は聞かないでください)と私が得た解決策はこれです:ScalaTest

exception.getClass shouldBe classOf[FileNotFoundException] 

をしかし、それはスーパーに見えます醜い、良い方法がありますか?

さようなら

答えて

1

一つの可能​​な解決策は、interceptメソッドを使用することです:

val exception = intercept[NoSuchElementException] { 
    List.empty[String].head // Code that throws exception 
} 

exception.getMessage shouldBe "head of empty list" 
1

あなたはできますが、マッチャー "[] thrownByでなければなりません":

an [IllegalArgumentException] should be thrownBy { 
    //code that should raise an exception here 
} 

あなたのことを確認してくださいテストクラスには "Matchers"が含まれます:

class MyTestClass extends FunSuite with Matchers 
関連する問題