haskellintegration-testingyesodyesod-test

How is ydescribe from Yesod.Test used?


I am trying to write tests where one test signs in and then the following tests use the cookie set from the first sign in test.

From what I can see, this is done with ydescribe

Start describing a Tests suite keeping cookies and a reference to the tested Application and ConnectionPool

I attempted to use this in my app by looking at some examples from github repos but had no luck.

Here's my code:

module Handler.PostSpec ( spec ) where

import TestImport hiding (postBody)
import Data.Aeson
import Yesod.Test

spec :: Spec
spec =
    ydescribe "Auth" $ do
        yit "logs in to dummy auth" $ do
            request $ do
                addPostParam "ident" "0"
                setMethod "POST"
                setUrl ("http://localhost:3000/auth/page/dummy" :: Text)
            statusIs 303

When compiled this gives the error:

test/Handler/PostSpec.hs:9:5: error:
    • Couldn't match type ‘transformers-0.5.2.0:Control.Monad.Trans.Writer.Lazy.WriterT
                             [YesodSpecTree site0] Identity ()’
                     with ‘hspec-core-2.4.4:Test.Hspec.Core.Spec.Monad.SpecM () ()’
      Expected type: Spec
        Actual type: YesodSpec site0
    • In the expression:
        ydescribe "Auth"
        $ do { yit "logs in to dummy auth"
               $ do { request $ do { ... };
                      statusIs 303 } }

      In an equation for ‘spec’:
          spec
            = ydescribe "Auth"
              $ do { yit "logs in to dummy auth"
                     $ do { request $ ...;
                            .... } }

What would be the correct way to use ydescribe?


Solution

  • Found out I have to use yesodSpec as well and pass ydescribe as the second parameter.

    Here is a usage example.

    spec :: Spec
    spec = do
        settings <-
            runIO $ loadYamlSettings
                ["config/test-settings.yml", "config/settings.yml"]
                []
                useEnv
        foundation <- runIO $ makeFoundation settings
        yesodSpec foundation $ do
            ydescribe "Tests" $ do
                yit "Test" $ do
                   -- Test something