【问题标题】:How to use Hspec/Yesod.Test with Sqlite?如何将 Hspec/Yesod.Test 与 Sqlite 一起使用?
【发布时间】:2021-08-28 08:42:15
【问题描述】:

我有一个使用 Sqlite 数据库的基本 Yesod 服务器,我正在尝试向它添加测试。 我对 Haskell 很陌生,所以我什至很难找到正确的术语,所以对我来说很简单。

我有这个代码来运行服务器:

runShortblitoWebServer :: Settings -> IO ()
runShortblitoWebServer Settings {..} =
  runStderrLoggingT $
    withSqlitePool "urls.db" 1 $ \pool -> do
      let app =
            App
              { appLogLevel = settingLogLevel,
                appStatic = shortblitoWebServerStatic,
                appConnectionPool = pool,
                appGoogleAnalyticsTracking = settingGoogleAnalyticsTracking,
                appGoogleSearchConsoleVerification = settingGoogleSearchConsoleVerification
              }
      flip runSqlPool pool $ do
        runMigration migrateTables
      liftIO $ Yesod.warp settingPort app

现在我正在尝试使用yesodSpecWithSiteGeneratoryesodSpecWithSiteGeneratorAndArgument 创建我的Spec。像这样的:

shortblitoWebServerSpec :: ShortblitoWebServerSpec -> SpecWith a
shortblitoWebServerSpec =
  yesodSpecWithSiteGeneratorAndArgument $
    withSqlitePool ":memory:" 1 $ \pool -> do
      let app =
            App
              { appLogLevel = LevelWarn,
                appStatic = shortblitoWebServerStatic,
                appConnectionPool = pool,
                appGoogleAnalyticsTracking = Nothing,
                appGoogleSearchConsoleVerification = Nothing
              }
      flip runSqlPool pool $ do
        runMigration migrateTables
      app

我收到了错误:

    • Couldn't match expected type ‘a -> IO App’ with actual type ‘App’
    • In a stmt of a 'do' block: app
      In the expression:
        do let app = ...
           flip runSqlPool pool $ do runMigration migrateTables
           app
      In the second argument of ‘($)’, namely
        ‘\ pool
           -> do let ...
                 flip runSqlPool pool $ do ...
                 ....’
    • Relevant bindings include
        shortblitoWebServerSpec :: ShortblitoWebServerSpec -> SpecWith a
          (bound at test/Shortblito/Web/Server/TestUtils.hs:49:1)
   |
62 |       app
   |       ^^^

我不知道该怎么办。我一直在尝试不同的东西,但此时我真的只是在黑暗中拍摄。

有人知道怎么做吗?您需要额外的信息吗?

谢谢!

【问题讨论】:

    标签: haskell yesod hspec yesod-test


    【解决方案1】:

    我想我明白了!

    这行得通:

    shortblitoWebServerSpec :: ShortblitoWebServerSpec -> Spec
    shortblitoWebServerSpec =
      yesodSpecWithSiteGenerator $
        runNoLoggingT $
          withSqlitePool ":memory:" 1 $ \pool -> do
            let app =
                  App
                    { appLogLevel = LevelWarn,
                      appStatic = shortblitoWebServerStatic,
                      appConnectionPool = pool,
                      appGoogleAnalyticsTracking = Nothing,
                      appGoogleSearchConsoleVerification = Nothing
                    }
            flip runSqlPool pool $
              do
                runMigration migrateTables
            return app
    

    我添加了runNoLoggingT,现在我正在使用yesodSpecWithSiteGenerator。如果以后需要,可以使用yesodSpecWithSiteGeneratorAndArgument

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-11
      • 2011-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-09
      • 2021-03-22
      相关资源
      最近更新 更多