ISC licensed and maintained by Alexis King
This version can be pinned in stack with:monad-mock-0.2.0.0@sha256:16a15283f89f63c52ec60ef4bbd3f815359edf21f498a142d91ee6196ff727d2,2585

Module documentation for 0.2.0.0

monad-mock Build Status

monad-mock is a Haskell package that provides a monad transformer to help create “mocks” of mtl-style typeclasses, intended for use in unit tests. A mock can be executed by providing a sequence of expected monadic calls and their results, and the mock will verify that the computation conforms to the expectation.

For example, imagine a MonadFileSystem typeclass, which describes a class of monads that may perform filesystem operations:

class Monad m => MonadFileSystem m where
  readFile :: FilePath -> m String
  writeFile :: FilePath -> String -> m ()

Using MockT, it’s possible to test computations that use MonadFileSystem in a completely pure way:

copyFile :: MonadFileSystem m => FilePath -> FilePath -> m ()
copyFile a b = do
  x <- readFile a
  writeFile b x

makeMock "FileSystemAction" [ts| MonadFileSystem |]

spec = describe "copyFile" $
  it "reads a file and writes its contents to another file" $
    evaluate $ copyFile "foo.txt" "bar.txt"
      & runMock [ ReadFile "foo.txt" :-> "contents"
                , WriteFile "bar.txt" "contents" :-> () ]

For more information, see the documentation on Hackage.

Changes

0.2.0.0 (September 14th, 2017)

  • Control.Monad.Mock.TH is smarter about deriving instances for classes with superclass contexts: the derived context is based on superclasses rather than being hardcoded to Monad m.

0.1.1.2 (August 1st, 2017)

  • Added support for GHC 7.10.

0.1.1.1 (June 28th, 2017)

  • Added support for GHC 8.2.
  • Includes some minor documentation fixes.

0.1.1.0 (June 27th, 2017)

  • Added Control.Monad.Mock.TH, which provides functions for automatically generating actions using Template Haskell.

0.1.0.0 (June 23rd, 2017)

  • Initial release