尝试单元化测试路由时出现问题。Scotty、Persistent和Hspec WAI。
与Yesod或Spock不同,Scotty没有一个很好的地方来存储数据库处理程序。我通过一个巨大的"do"来启动数据库,将数据库池保持为本地变量,然后使用该变量。
app :: IO ()
app = do
-- allocate_database $ pool
-- scotty 8080 $do
-- handleSomeRoute pool
然而,Hspec WAI希望它出现在IO申请表中
scottyApp :: ScottyM () -> IO Application
有没有一种合理的方法将DB连接池注入scottyApp?
以下是如何做到这一点。基本上,在进行hspec
调用之前打开数据库:
{-# LANGUAGE OverloadedStrings #-}
import Test.Hspec
import Test.Hspec.Wai
import Network.Wai (Application)
import qualified Web.Scotty as S
allocate_db :: (Int -> IO a) -> IO a
allocate_db = undefined
handleSomeRoute :: Int -> S.ScottyM ()
handleSomeRoute = undefined
main2 :: IO ()
main2 = allocate_db $ pool -> do
let app' = handleSomeRoute pool
hspec $ with (S.scottyApp app') $ do
describe "GET /" $ do
it "responds with 200" $ do
get "/" `shouldRespondWith` 200