在yesod test

时间:2017-12-30 05:04:12

标签: haskell testing integration-testing yesod yesod-test

在我的yesod测试中,我希望能够在测试过程中修改数据库中的记录。

以下是我提出的代码

        yit "post is created by authorized user" $ do
            request $ do
                addPostParam "ident" "dummy"
                setMethod "POST"
                setUrl ("http://localhost:3000/auth/page/dummy" :: Text)
            update 0 [UserAuthorized =. True]
            postBody PostR (encode $ object [
                "body" .= ("test post" :: Text),
                "title" .= ("test post" :: Text),
                "coverImage" .= ("test post" :: Text),
                "author" .= (0 :: Int)
                ])
            statusIs 200

此操作失败并显示错误

• Couldn't match expected type ‘IO a0’
              with actual type ‘ReaderT backend0 m0 ()’
• In the second argument of ‘($)’, namely
    ‘update 0 [UserAuthorized =. True]’

  In a stmt of a 'do' block:
    runIO $ update 0 [UserAuthorized =. True]
  In the expression:
    do { settings <- runIO
                     $ loadYamlSettings
                         ["config/test-settings.yml", "config/settings.yml"] [] useEnv;
         foundation <- runIO $ makeFoundation settings;
         yesodSpec foundation $ do { ydescribe "Auth" $ do { ... } };
         runIO $ update 0 [UserAuthorized =. True];
         .... }

我可以说这是因为update返回m ()而非YesodExample site (),例如requestpostBodystatusIs

我如何能够在此测试中进行数据库更新?

2 个答案:

答案 0 :(得分:2)

您需要使用SELECT * FROM PESAN GROUP BY ID_PENGIRIM ORDER BY TANGGAL_PESAN SELECT ... min(date) FROM ... GROUP BY ... ORDER BY min(...) 功能。即:

runDB

答案 1 :(得分:0)

这有两个问题,第一个是Sibi指出我需要runDB第二个是你不能只用一个整数查找记录。

为了实现这一点,我使用了以下代码

runDB $ do
    (first :: Maybe (Entity User)) <- selectFirst [] []
    case first of
        Nothing -> pure () -- handle the case when the table is empty
        Just (Entity k _) -> update k [UserAuthorized =. True]

此查找是DB中的记录,然后更新它。修改(first :: Maybe (Entity User)) <- selectFirst [] []以选择要更新的记录。