mardi 18 septembre 2018

Testing happstack application with hspec

I''m trying to figure out how to test a happstack application. I implemented a simple function to create an response for my server

entryPage :: Int -> ServerPartT IO Response
entryPage i = ok $ toResponse ("return " ++ show 1)

run :: IO ()
run = simpleHTTP nullConf (entryPage i)

now i want to test the function entryPage. For example that the response is indeed return x. I created a hspec function to test it:

spec :: Spec
spec = describe "TestPage" $
    it "entryPage" $  do
        response <- entryPage 1
        let responseBody = rsBody response
        "return 1" `shouldBe` show responseBody

But the compiler reject it with the error:

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’

I can not get any further here.

From do response <- entryPage 1 I open the ServerPartT Monad and do not know how to leave it again. Or is the approach wrong? I know that there is no generic way to escape a monad, but is there a solution for my problem?

Aucun commentaire:

Enregistrer un commentaire