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

Test.MockCat.WithMock

Description

This module provides a declarative way to define mock functions with expectations that are automatically verified when the withMock block completes.

Example:

withMock $ do
  mockFn mock (any ~ True)
    expects do
      called once with "a"
  
  evaluate $ mockFn "a"
Synopsis

Documentation

withMock :: ReaderT WithMockContext IO a -> IO a Source #

Run a block with mock expectations that are automatically verified

expects :: forall m fn exp params. (MonadIO m, MonadWithMockContext m, ResolvableMock fn, ResolvableParamsOf fn ~ params, ExtractParams exp, ExpParams exp ~ params, BuildExpectations fn exp, Show params, Eq params) => m fn -> exp -> m fn infixl 0 Source #

called :: Called params => TimesSpec -> Expectations params () Source #

with :: WithArgs exp args params => exp -> args -> Expectations params () Source #

calledInOrder :: CalledInOrder args params => args -> Expectations params () Source #

calledInSequence :: CalledInSequence args params => args -> Expectations params () Source #

times :: Int -> TimesSpec Source #

Create a times condition for exact count.

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

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'.

atLeast :: Int -> TimesSpec Source #

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

f `shouldBeCalled` atLeast 1

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

newtype WithMockContext Source #

Mock expectation context holds verification actions to run at the end of the withMock block. Storing `IO ()` avoids forcing concrete param types at registration time.

Constructors

WithMockContext (TVar [IO ()]) 

data Expectation params where Source #

Expectation specification

Constructors

CountExpectation :: forall params. CountVerifyMethod -> params -> Expectation params

Count expectation with specific arguments

CountAnyExpectation :: forall params. CountVerifyMethod -> Expectation params

Count expectation without arguments (any arguments)

OrderExpectation :: forall params. VerifyOrderMethod -> [params] -> Expectation params

Order expectation

SimpleExpectation :: forall params. params -> Expectation params

Simple expectation (at least once) with arguments

AnyExpectation :: forall params. Expectation params

Simple expectation (at least once) without arguments

newtype Expectations params a Source #

Expectations builder (Monad instance for do syntax)

Constructors

Expectations (State [Expectation params] a) 

Instances

Instances details
Applicative (Expectations params) Source # 
Instance details

Defined in Test.MockCat.WithMock

Methods

pure :: a -> Expectations params a #

(<*>) :: Expectations params (a -> b) -> Expectations params a -> Expectations params b #

liftA2 :: (a -> b -> c) -> Expectations params a -> Expectations params b -> Expectations params c #

(*>) :: Expectations params a -> Expectations params b -> Expectations params b #

(<*) :: Expectations params a -> Expectations params b -> Expectations params a #

Functor (Expectations params) Source # 
Instance details

Defined in Test.MockCat.WithMock

Methods

fmap :: (a -> b) -> Expectations params a -> Expectations params b #

(<$) :: a -> Expectations params b -> Expectations params a #

Monad (Expectations params) Source # 
Instance details

Defined in Test.MockCat.WithMock

Methods

(>>=) :: Expectations params a -> (a -> Expectations params b) -> Expectations params b #

(>>) :: Expectations params a -> Expectations params b -> Expectations params b #

return :: a -> Expectations params a #