当前位置:  开发笔记 > 编程语言 > 正文

Unit testing IO actions with Hspec

如何解决《UnittestingIOactionswithHspec》经验,为你挑选了1个好方法。

I have found other questions on similar lines but nothing that answers my question in this particular scenario. Furthermore, there seem to be few resources which succinctly cover the subject of unit testing IO actions in Haskell.

Let's say I have this typeclass for my database communication:

data Something = Something String deriving Show

class MonadIO m => MonadDB m where
  getSomething :: String -> m Something
  getSomething s = do
    ... -- assume a DB call is made and an otherwise valid function

instance MonadDB IO 

and this function which uses it:

getIt :: MonadDB m => m (Int, Something)
getIt = do
  s@(Something str) <- getSomething "hi"
  return (length str, s) -- excuse the contrived example

I wish to test this getIt function with hspec but without it talking to the database, which presumably means replacing which MonadDB it uses, but how do I achieve that?



1> David McHeal..:

这对您有用吗?

#!/usr/bin/env stack
-- stack exec --package transformers --package hspec -- ghci
import Control.Monad.IO.Class
import Control.Monad.Trans.Identity

import Data.Char
import Test.Hspec

data Something = Something String deriving (Eq, Show)

class MonadIO m => MonadDB m where
  getSomething :: String -> m Something
  getSomething s = return $ Something (map toUpper s)

instance MonadDB IO

instance MonadIO m => MonadDB (IdentityT m)

getIt :: MonadDB m => m (Int, Something)
getIt = do
  s@(Something str) <- getSomething "hi"
  return (length str, s)

main :: IO ()
main = hspec $ do
  describe "Some tests" $ do
    it "test getIt" $ do
      runIdentityT getIt `shouldReturn` (2, Something "HI")

    it "test getIt should fail" $ do
      runIdentityT getIt `shouldReturn` (1, Something "HI")

您也许还可以使用ReaderTStateT“提供”数据或转换getSomething以供在测试查询时使用。

编辑:在hspec中使用示例。

推荐阅读
oDavid_仔o_880
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有