我正在尝试弄清楚如何测试happstack应用程序。我实现了一个简单的功能来为服务器创建响应
entryPage :: Int -> ServerPartT IO Response
entryPage i = ok $ toResponse ("return " ++ show 1)
run :: IO ()
run = simpleHTTP nullConf (entryPage i)
现在我要测试功能entryPage
。例如,响应确实是return x
。我创建了一个hspec函数来对其进行测试:
spec :: Spec
spec = describe "TestPage" $
it "entryPage" $ do
response <- entryPage 1
let responseBody = rsBody response
"return 1" `shouldBe` show responseBody
但是编译器拒绝它并显示错误:
Couldn't match type ‘IO’ with ‘ServerPartT IO’
Expected type: ServerPartT IO ()
Actual type: Expectation
• In a stmt of a 'do' block:
"return 1" `shouldBe` show responseBody
In the second argument of ‘($)’, namely
‘do response <- entryPage 1
let responseBody = rsBody response
"return 1" `shouldBe` show responseBody’
In the second argument of ‘($)’, namely
‘it "entryPage"
$ do response <- entryPage 1
let responseBody = rsBody response
"return 1" `shouldBe` show responseBody’
我在这里无法获得任何帮助。
在do response <- entryPage 1
中,我打开了ServerPartT Monad,但不知道如何再次保留它。还是方法错误?我知道没有通用的方法可以逃脱monad,但是我的问题有解决方案吗?