2017-09-20 30 views
3

如果我有一個導致某個輸入發生錯誤的函數,是否可以編寫一個測試來驗證該輸入發生錯誤?是否可以在HUnit中聲明錯誤情況?

我在HUnit中找不到「assert error」功能。它在HUnit中還是可能在其他一些測試包中可用?

+2

的'HUnit-Plus'包提供['assertThrows'(https://開頭hackage。 haskell.org/package/HUnit-Plus-2.0.0/docs/Test-HUnitPlus-Base.html#v:assertThrows)。 –

回答

4

您可以捕獲錯誤並聲稱如果不發生使用標準異常處理:

errored <- catch (somethingThatErrors >> pure False) handler 
if errored then 
    assertFailure "Did not catch expected error" 
else 
    pure() 
where 
    handler :: ErrorCall -> IO Bool 
    handler _ = pure True 
相關問題