是否可以在IO以外的monad中使用带有测试框架的HUnit?

时间:2012-11-02 14:17:19

标签: haskell hunit

我目前有以下测试代码:

testUpdate :: Test
testUpdate = testCase "update does change artist" $ do
  (created, Just revised, parents) <- mbTest $ do
    Just editor <- fmap entityRef <$> findEditorByName "acid2"

    created <- create editor startWith
    let artistId = coreMbid created

    newRev <- update editor (coreRevision created) expected

    editId <- openEdit
    includeRevision editId newRev
    apply editId

    found <- findLatest artistId
    parents <- revisionParents newRev

    return (created, found, parents)

  coreData revised @?= expected

  assertBool "The old revision is a direct parent of the new revision" $
    parents == [coreRevision created]

  where
    startWith = ...
    expected = ...

这种方法有效,但它很混乱。我更愿意写一些东西,而不必返回被测试的各种东西,而是让它们有意义的断言。

我看到有Assertable类,但似乎我可能不得不重新发明一堆东西。

1 个答案:

答案 0 :(得分:1)

为什么不让你的monad返回IO a类型的IO计算,这是值? 因为在你的评论中,monad是MonadIO实例的情况是微不足道的, 假设monad允许纯计算:

newtype M a = M {runM :: ....}
instance Monad M where
  ...

makeTest :: M Assertion
makeTest = do
    created <- ..
    found   <- ..
    parents <- ..
    let test1 = coreData revised @?= expected
    ...
    let test2 = assertBool "The old revision..." $
                   parents == [coreRevision create]

    return $ test1 >> test2

testUpdate :: Test
testUpdate = testCase "update does change artist" $ runM makeTest

奖励是你可以通过一个monadic计算返回一组测试, 就像你在monad列表中那样。