在Sqlite中使用Hspec/Yesod.Test,可以按照以下步骤进行:
cabal install hspec yesod-test
TestSpec.hs
。在该文件中,导入所需的模块:import Test.Hspec
import Yesod.Test
import Database.Persist.Sqlite
import Control.Monad.IO.Class (liftIO)
import MyApp (withApp)
spec :: Spec
spec = withApp $ do
describe "GET /users" $ do
it "responds with 200" $ do
get ("/users" :: Text)
statusIs 200
runDB
函数来执行数据库操作。以下是一个示例:withApp :: SpecWith (TestApp App) -> Spec
withApp = before $ do
pool <- liftIO $ createSqlitePool ":memory:" 10
let settings = defaultSettings { connectionPool = pool }
app <- liftIO $ withAppSettings settings $ return . fst
return (TestApp app)
main :: IO ()
main = hspec spec
云原生正发声
Elastic 实战工作坊
企业创新在线学堂
DBTalk技术分享会
Techo Day
云+社区技术沙龙[第9期]
企业创新在线学堂
领取专属 10元无门槛券
手把手带您无忧上云