typeable-mock: Mock functions and expressions anywhere.

[ bsd3, library, testing ] [ Propose Tags ] [ Report a vulnerability ]

Please see the README on GitHub at https://github.com/lykahb/typeable-mock#readme


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.1.0.0, 0.1.0.1
Change log ChangeLog.md
Dependencies base (>=4.7 && <5), call-stack (>=0.2.0), containers, variadic-function (>=0.1.0.0 && <0.2) [details]
License BSD-3-Clause
Copyright 2021 Boris Lykah
Author Boris Lykah
Maintainer lykahb@gmail.com
Category Testing
Home page https://github.com/lykahb/typeable-mock#readme
Bug tracker https://github.com/lykahb/typeable-mock/issues
Source repo head: git clone https://github.com/lykahb/typeable-mock
Uploaded by BorisLykah at 2021-07-05T18:05:17Z
Distributions NixOS:0.1.0.1
Downloads 333 total (11 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2021-07-05 [all 1 reports]

Readme for typeable-mock-0.1.0.1

[back to package description]

Mock any Typeable function or expression anywhere.

A common approach to mocking in Haskell is with type classes, with instances for the real and test logic. Typeable-mock fills a niche for the projects that do not use granular type classes so much. It lets you mock any Typeable expression in context of nearly any monad. It works in a monad of a concrete type or a polymorphic one with class constraints.

-- Use mock in application. Here `useMock` is a user-written helper that
-- is aware of the application context and can look up mocks in there.
useMock "writeFile" writeFile >>= \f -> liftIO (f path contents)

-- Declare mock in test.
writeFileMock <- makeMock "writeFile"
  ((\_ _ -> pure ()) :: FilePath -> String -> IO ())

-- Check assertions
assertHasCalls
  [ expectCall "/tmp/1.txt" "Hello world",
    expectCall AnyVal (PredicateVal $ elem "Hello" . words)
  ]
  writeFileMock

See the package documentation and examples/App.hs for more.