在 Haskell 中编写一个单元测试,其中undefined
遇到表达式应该失败的地方有点棘手。我用 HSpec 尝试了以下操作:
module Main where
import Test.Hspec
import Control.Exception (evaluate)
main :: IO ()
main = hspec $ do
describe "Test" $ do
it "test case" $ do
evaluate (take 1 $ map (+1) [undefined, 2, 3]) `shouldThrow` anyException
无济于事。它报告我did not get expected exception: SomeException
如果我在 REPL 中评估相同的表达式,我会得到:
[*** Exception: Prelude.undefined
CallStack (from HasCallStack):
error, called at libraries\base\GHC\Err.hs:79:14 in base:GHC.Err
undefined, called at <interactive>:2:20 in interactive:Ghci1