我正在尝试使用该功能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)