我试图使用函数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)
答案 0 :(得分:1)
问题最终是在posts表中有一个列体,所以Yesod为此创建了postBody,它与Yesod.Test中的postBody冲突。
解决方案是在yesod测试中使用限定导入
import qualified Yesod.Test as T