8

我目前有以下测试代码:

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课程,但似乎我最终可能不得不重新发明一堆东西。

4

1 回答 1

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

一个好处是您可以通过一个单子计算返回一组测试,就像在列表单子中一样。

于 2012-11-10T08:11:11.767 回答