Created
March 22, 2017 02:33
-
-
Save boccato/e151d3a4e4f5d925b904126f80d8cb65 to your computer and use it in GitHub Desktop.
combine the with app and around withRollback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
module Main where | |
import Control.Exception (bracket) | |
import Network.Wai | |
import Network.HTTP.Types (status200) | |
import Test.Hspec | |
import Test.Hspec.Wai | |
main :: IO () | |
main = hspec spec | |
-- Wai Application | |
app :: Application | |
app req send = send $ responseLBS status200 [] "" | |
-- | |
type Connection = Int | |
openConnection :: IO Connection | |
openConnection = do | |
putStrLn "openConnection" | |
return 42 | |
closeConnection :: Connection -> IO () | |
closeConnection c = do | |
putStrLn "closeConnection" | |
return () | |
withDatabaseConnection :: ActionWith Connection -> IO () | |
withDatabaseConnection = bracket openConnection closeConnection | |
withRollback :: ActionWith Connection -> IO () | |
withRollback action = withDatabaseConnection $ \c -> do | |
putStrLn $ "before: " ++ show c | |
action c | |
putStrLn $ "after: " ++ show c | |
-- Works | |
-- spec :: Spec | |
-- spec = with (return app) $ | |
-- describe "GET /" $ | |
-- it "responds with 200" $ | |
-- get "/" `shouldRespondWith` 200 | |
-- Works! | |
-- spec :: Spec | |
-- spec = around withDatabaseConnection $ | |
-- describe "Uses a connection with rollback!" $ | |
-- it "can access the connection" $ \conn -> | |
-- conn `shouldBe` 42 | |
makeApp :: Connection -> IO Application | |
makeApp c = do | |
putStrLn $ "makeApp: " ++ show c | |
return app | |
-- Doesn't work | |
-- spec :: Spec | |
-- spec = around withRollback $ | |
-- beforeWith makeApp $ | |
-- describe "Uses a connection with rollback!" $ | |
-- it "can access the connection" $ \conn -> | |
-- conn `shouldBe` 42 | |
-- Works but I can't access the connection inside the test. | |
spec :: Spec | |
spec = around withRollback $ | |
beforeWith makeApp $ | |
describe "Uses a connection with rollback!" $ | |
it "can access the connection" $ | |
get "/" `shouldRespondWith` 200 | |
-- http://hspec.github.io/writing-specs.html | |
-- https://begriffs.com/posts/2014-10-19-warp-server-controller-test.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment