typeable-mock: Mock functions and expressions anywhere.

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.

[maintain] [Publish]

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


[Skip to Readme]

Properties

Versions 0.1.0.0, 0.1.0.1, 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-05T04:56:11Z

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


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.