0

我正在尝试使用该功能postBody

postBody :: (Yesod site, RedirectUrl site url) => url -> ByteString -> YesodExample site () 

来自Yesod.Test包。文档说可以这样使用

import Data.Aeson
postBody HomeR (encode $ object ["age" .= (1 :: Integer)])

但是,当我尝试在我的应用程序中使用它时

    describe "Posts" $ do
        it "posts post and returns post" $ do
            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 ‘Post’ with actual type ‘Route App’
• In the first argument of ‘postBody’, namely ‘PostR’
  In a stmt of a 'do' block:
    postBody
      PostR
      (encode
       $ object
           ["body" .= ("test post" :: Text), "title" .= ("test post" :: Text),

            "coverImage" .= ("test post" :: Text), "author" .= (0 :: Int)])
  In the second argument of ‘($)’, namely
    ‘do { postBody
            PostR
            (encode
             $ object
                 ["body" .= ("test post" :: Text), "title" .= ("test post" :: Text),
                  ....]);
          statusIs 200 }’

我的用法似乎与示例相同,所以我看不出它为什么会失败。

PostR 在此处的路由文件中

/posts PostR POST

和它的处理程序

postPostR :: Handler Value
postPostR = do
    post <- requireJsonBody :: Handler Post
    maybeUserID <- maybeAuthId
    case maybeUserID of
        Just userID -> do
            let post' = post {postAuthor = userID}
            inserted <- runDB $ insertEntity post'
            returnJson inserted
        Nothing -> sendResponseStatus status403 ("Unauthorized" :: Text)
4

1 回答 1

1

问题最终是在posts表中有一个列体,所以Yesod为此创建了postBody,它与Yesod.Test的postBody冲突。

解决方案是在 yesod 测试中使用合格的导入

import qualified Yesod.Test as T
于 2017-12-19T21:28:32.180 回答