-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Documentation for using wai-hspec with other resources #36
Comments
To clarify, I've read the writing specs tutorial, but it doesn't answer questions like how to:
If all of this seems obvious, then that's exactly why publishing an example would help so much. What's obvious to one is often murky to another. |
Doesn't reset the DB around each test yet. waiTests :: IO TestTree
waiTests = do
dbVar <- newEmptyMVar
testSpec "wai-tests" $ beforeAll_ (startDB dbVar) $ afterAll_ (stopDB dbVar) $ (before (makeTestApp <$> getConfig dbVar)) $ do
describe "/my-endpoint" $ do
it "loves being posted to" $ do
config <- liftIO (getConfig dbVar)
user <- makeArbitraryUser config
post "/my-endpoint"
(fromValue $ object [ "name" .= (userName user), "message" .= "peace and love" ])
`shouldRespondWith`
(fromValue $ object [ "response" .= "mostly agree" ])
where
startDB var = do
db <- makeDatabase "path/to/schema.sql"
putMVar var db
stopDB var = do
db <- takeMVar var
stopPostgres db
getConfig var = do
db <- readMVar var
pure $ Config { dbConnection = connection db }
-- | Config is the configuration for the whole WAI app, specific to this app.
data Config = Config { dbConnection :: Connection }
-- | User is custom user object specific to this app.
data User = User { userName :: Text }
-- | makeTestApp constructs our application from its configuration.
makeTestApp :: Config -> Application
makeTestApp = serve api (server config)
-- | makeArbitrary user inserts a new user into the database and returns the newly-created user
makeArbitraryUser :: MonadIO => Config -> m User
makeArbitraryUser config = undefined -- use `config` to insert something into the database |
Hi, sorry for the late reply. I don't have much time to put into this, but here are some pointers. Before we start, a general disclaimer (you are probably already aware of this, but I leave it here for others who might read this in the future):
Currently If you want to use the connection within an acceptance test things get more complicated. You can't use You can try to piece things together by following the types. |
Example for using {-# LANGUAGE OverloadedStrings #-}
module AppSpec (spec) where
import Test.Hspec
import Test.Hspec.Wai
import Network.Wai
data Connection
mkApplication :: Connection -> Application
mkApplication = undefined
newConnection :: IO Connection
newConnection = undefined
closeConnection :: Connection -> IO ()
closeConnection = undefined
createUser :: Connection -> IO ()
createUser = undefined
withApplication :: ActionWith Application -> ActionWith Connection
withApplication action connection = do
action (mkApplication connection)
withConnection :: ActionWith Connection -> (Connection -> IO ()) -> ActionWith Connection
withConnection action f connection = do
f connection
action connection
spec :: Spec
spec = beforeAll newConnection $ afterAll closeConnection $ do
aroundWith (withConnection createUser) $ do
aroundWith withApplication $ do
describe "/" $ do
it "responds with 200" $ do
get "/" `shouldRespondWith` 200 |
Example for using {-# LANGUAGE OverloadedStrings #-}
module AppSpec (spec) where
import Test.Hspec
import Test.Hspec.Wai
import Test.Hspec.Wai.Internal
import Network.Wai
data Connection
mkApplication :: Connection -> Application
mkApplication = undefined
newConnection :: IO Connection
newConnection = undefined
closeConnection :: Connection -> IO ()
closeConnection = undefined
createUser :: Connection -> IO ()
createUser = undefined
spec :: Spec
spec = beforeAll newConnection $ afterAll closeConnection $ do
describe "/" $ do
it "responds with 200" $ \connection -> do
createUser connection
withApplication (mkApplication connection) $ do
get "/" `shouldRespondWith` 200 |
One more note, a convenient way to reset the database into a pristine state is to create a transaction before each test and do a rollback after. |
Thank you! It'll take me a little while to digest those, but they're very much appreciated. |
Finally got back to this. It all pieces together now. I don't think I would ever have figured out Thank you. |
Thanks to @sol for the help: hspec/hspec-wai#36
I'd like to use hspec-wai in the following way:
I think this is a pretty standard set of things to want to test. However, it's really hard to figure out how to do this with hspec & hspec-wai. I'm cobbling something together now (will post if I finish it), but it has taken a lot of time to figure it out, with a lot of searching down dead ends.
I would really appreciate concrete examples for doing the above. I think they'd make a good addition to the documentation.
The text was updated successfully, but these errors were encountered: