| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Test.MockCat.Mock
Description
Utilities for constructing verifiable stub functions. This module provides the core functions for creating mocks and stubs.
Key Functions
Synopsis
- class MockBuilder params fn verifyParams | params -> fn, params -> verifyParams
- buildMock :: (MonadIO m, MockBuilder params fn verifyParams) => Maybe MockName -> params -> m (BuiltMock fn verifyParams)
- mock :: CreateMockFn a => a
- mockM :: CreateMockFnM a => a
- createNamedMockFnWithParams :: (MonadIO m, MockBuilder params fn verifyParams, Typeable verifyParams, Typeable fn) => MockName -> params -> m fn
- stub :: CreateStubFn a => a
- shouldBeCalled :: ShouldBeCalled m spec => m -> spec -> IO ()
- times :: Int -> TimesSpec
- atLeast :: Int -> TimesSpec
- atMost :: Int -> TimesSpec
- greaterThan :: Int -> TimesSpec
- lessThan :: Int -> TimesSpec
- once :: TimesSpec
- never :: TimesSpec
- inOrder :: OrderSpec
- inPartialOrder :: OrderSpec
- inOrderWith :: (ToNormalizedArg params, Eq (NormalizeWithArg params), Show (NormalizeWithArg params)) => [params] -> VerificationSpec (NormalizeWithArg params)
- inPartialOrderWith :: (ToNormalizedArg params, Eq (NormalizeWithArg params), Show (NormalizeWithArg params)) => [params] -> VerificationSpec (NormalizeWithArg params)
- calledWith :: params -> VerificationSpec params
- anything :: VerificationSpec params
- withArgs :: (ToNormalizedArg params, Eq (NormalizeWithArg params), Show (NormalizeWithArg params)) => TimesSpec -> params -> VerificationSpec (NormalizeWithArg params)
- onCase :: a -> Cases a ()
- cases :: [a] -> Cases a ()
- casesIO :: [a] -> Cases (IO a) ()
- label :: MockName -> Label
- data Label
Documentation
class MockBuilder params fn verifyParams | params -> fn, params -> verifyParams Source #
Class for creating a stub corresponding to the parameter description.
Minimal complete definition
Instances
| MockBuilder (Param r) r () Source # | Instance for building a stub for a value (backward compatibility). |
| MockBuilder (IO r) (IO r) () Source # | Instance for building a stub for a constant IO action. |
| MockBuilder (Head :> Param r) r () Source # | Instance for building a stub for a constant value (with Head marker). |
| (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 |
| (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 |
| MockBuilder (Cases (IO a) ()) (IO a) () Source # | Instance for building a stub for `Cases (IO a) ()`. |
buildMock :: (MonadIO m, MockBuilder params fn verifyParams) => Maybe MockName -> params -> m (BuiltMock fn verifyParams) Source #
mock :: CreateMockFn a => a Source #
Create a mock function with verification hooks attached.
This function can be used in two ways:
- Without a name:
f mock $ "a" ~ "b" - 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.
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:
- Without a name:
let f = stub $ "a" ~> "b" - 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 (>).
Create a times condition for never (zero times). Equivalent to 'times 0'.
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 #