mockcat-1.0.1.0: Declarative mocking with a single arrow `~>`.
Safe HaskellNone
LanguageHaskell2010

Test.MockCat.Mock

Description

Utilities for constructing verifiable stub functions. This module provides the core functions for creating mocks and stubs.

Key Functions

  • mock: Create a verifiable mock function (records calls).
  • stub: Create a pure stub function (no recording).
  • mockM: Create a monadic mock function (allows explicit side effects).
Synopsis

Documentation

class MockBuilder params fn verifyParams | params -> fn, params -> verifyParams Source #

Class for creating a stub corresponding to the parameter description.

Minimal complete definition

build

Instances

Instances details
MockBuilder (Param r) r () Source #

Instance for building a stub for a value (backward compatibility).

Instance details

Defined in Test.MockCat.Internal.Builder

Methods

build :: MonadIO m => Maybe MockName -> Param r -> m (BuiltMock r ()) Source #

MockBuilder (IO r) (IO r) () Source #

Instance for building a stub for a constant IO action.

Instance details

Defined in Test.MockCat.Internal.Builder

Methods

build :: MonadIO m => Maybe MockName -> IO r -> m (BuiltMock (IO r) ()) Source #

MockBuilder (Head :> Param r) r () Source #

Instance for building a stub for a constant value (with Head marker).

Instance details

Defined in Test.MockCat.Internal.Builder

Methods

build :: MonadIO m => Maybe MockName -> (Head :> Param r) -> m (BuiltMock r ()) Source #

(p ~ (Param a :> rest), ParamConstraints p args r, BuildCurried args r fn) => MockBuilder (Param a :> rest) fn args Source #

Overlapping instance for building a stub defined via chained Param.

Instance details

Defined in Test.MockCat.Internal.Builder

Methods

build :: MonadIO m => Maybe MockName -> (Param a :> rest) -> m (BuiltMock fn args) Source #

(ParamConstraints params args r, BuildCurried args r fn) => MockBuilder (Cases params ()) fn args Source #

Overlapping instance for building a stub when parameters are provided as Cases.

Instance details

Defined in Test.MockCat.Internal.Builder

Methods

build :: MonadIO m => Maybe MockName -> Cases params () -> m (BuiltMock fn args) Source #

MockBuilder (Cases (IO a) ()) (IO a) () Source #

Instance for building a stub for `Cases (IO a) ()`.

Instance details

Defined in Test.MockCat.Internal.Builder

Methods

build :: MonadIO m => Maybe MockName -> Cases (IO a) () -> m (BuiltMock (IO a) ()) Source #

buildMock :: (MonadIO m, MockBuilder params fn verifyParams) => Maybe MockName -> params -> m (BuiltMock fn verifyParams) Source #

New name for build to make intent explicit. buildMock constructs a mock function and its verifier.

mock :: CreateMockFn a => a Source #

Create a mock function with verification hooks attached.

This function can be used in two ways:

  1. Without a name: f mock $ "a" ~ "b"
  2. With a name (using label): f mock (label "mockName") $ "a" ~ "b"

The function creates a verifiable stub that records calls and can be verified via the unified shouldBeCalled API. The function internally uses unsafePerformIO to make the returned function appear pure, but it requires MonadIO for creation.

mockM :: CreateMockFnM a => a Source #

createNamedMockFnWithParams :: (MonadIO m, MockBuilder params fn verifyParams, Typeable verifyParams, Typeable fn) => MockName -> params -> m fn Source #

Internal function for TH code that already has MockBuilder constraint. This avoids CreateNamedMock instance resolution issues in generated code.

stub :: CreateStubFn a => a Source #

Create a pure stub function without verification hooks.

This function can be used in two ways:

  1. Without a name: let f = stub $ "a" ~> "b"
  2. With a name (using label): let f = stub (label "stubName") $ "a" ~> "b"

This function creates a simple stub that returns values based on the provided parameters, but does not support verification. Use mock if you need verification capabilities.

shouldBeCalled :: ShouldBeCalled m spec => m -> spec -> IO () Source #

times :: Int -> TimesSpec Source #

Create a times condition for exact count.

f `shouldBeCalled` times 3
f `shouldBeCalled` (times 3 `with` "arg")

atLeast :: Int -> TimesSpec Source #

Create a times condition for at least count (>=).

f `shouldBeCalled` atLeast 1

atMost :: Int -> TimesSpec Source #

Create a times condition for at most count (<=).

f `shouldBeCalled` atMost 2

greaterThan :: Int -> TimesSpec Source #

Create a times condition for greater than count (>).

lessThan :: Int -> TimesSpec Source #

Create a times condition for less than count (<).

once :: TimesSpec Source #

Create a times condition for exactly once. Equivalent to 'times 1'.

never :: TimesSpec Source #

Create a times condition for never (zero times). Equivalent to 'times 0'.

inOrder :: OrderSpec Source #

Create an order condition for exact sequence

inPartialOrder :: OrderSpec Source #

Create an order condition for partial sequence

inOrderWith :: (ToNormalizedArg params, Eq (NormalizeWithArg params), Show (NormalizeWithArg params)) => [params] -> VerificationSpec (NormalizeWithArg params) Source #

Verify that the mock was called with the specified sequence of arguments in exact order.

f `shouldBeCalled` inOrderWith ["a", "b"]

inPartialOrderWith :: (ToNormalizedArg params, Eq (NormalizeWithArg params), Show (NormalizeWithArg params)) => [params] -> VerificationSpec (NormalizeWithArg params) Source #

Verify that the mock was called with the specified sequence of arguments, allowing other calls in between.

f `shouldBeCalled` inPartialOrderWith ["a", "c"]
-- This passes if calls were: "a", "b", "c"

calledWith :: params -> VerificationSpec params Source #

Create a simple verification with arguments. This accepts both raw values and Param chains.

f `shouldBeCalled` calledWith "a"

anything :: VerificationSpec params Source #

Create a simple verification without arguments. It verifies that the function was called at least once, with ANY arguments.

f `shouldBeCalled` anything

withArgs :: (ToNormalizedArg params, Eq (NormalizeWithArg params), Show (NormalizeWithArg params)) => TimesSpec -> params -> VerificationSpec (NormalizeWithArg params) infixl 8 Source #

New function for combining times condition with arguments (supports raw values) This will replace with once the old with is removed

onCase :: a -> Cases a () Source #

Register a stub case within a Cases builder.

cases :: [a] -> Cases a () Source #

Define stub cases from a list of patterns.

casesIO :: [a] -> Cases (IO a) () Source #

IO variant of cases.

label :: MockName -> Label Source #

Label function for naming mock functions. Use it with mock to provide a name for the mock function.

  f mock (label "mockName") $ "a" ~ "b"
  

data Label Source #

Label type for naming mock functions.