IHP Api Reference
Safe HaskellNone

IHP.Test.Mocking

Synopsis

Documentation

type ContextParameters application = (?applicationContext :: ApplicationContext, ?context :: RequestContext, ?modelContext :: ModelContext, ?application :: application, InitControllerContext application, ?mocking :: MockContext application) Source #

withIHPApp :: InitControllerContext application => application -> ConfigBuilder -> (MockContext application -> IO ()) -> IO () Source #

Create contexts that can be used for mocking

mockContextNoDatabase :: InitControllerContext application => application -> ConfigBuilder -> IO (MockContext application) Source #

withContext :: (ContextParameters application => IO a) -> MockContext application -> IO a Source #

Run a IO action, setting implicit params based on supplied mock context

setupWithContext :: (ContextParameters application => IO a) -> MockContext application -> IO (MockContext application) Source #

callAction :: forall application controller. (Controller controller, ContextParameters application, Typeable application, Typeable controller) => controller -> IO Response Source #

Runs a controller action in a mock environment

callActionWithParams :: forall application controller. (Controller controller, ContextParameters application, Typeable application, Typeable controller) => controller -> [Param] -> IO Response Source #

Runs a controller action in a mock environment

>>> callActionWithParams CreatePostAction [("title", "Hello World"), ("body", "lorem ipsum")|
Response { .. }

callJob :: (ContextParameters application, Typeable application, Job job) => job -> IO () Source #

Run a Job in a mock environment

Example:

Let's say you have a Job called JobPost that you would like to process as part of a test.

 let postJob <- fetch ...

 callJob postJob

Note that callJob doesn't set the Job status that is initially set JobStatusNotStarted, as that is done by the Job queue (see jobDidSucceed for example).

mockAction :: forall application controller. (Controller controller, ContextParameters application, Typeable application, Typeable controller) => controller -> IO Response Source #

mockAction has been renamed to callAction

mockActionResponse :: forall application controller. (Controller controller, ContextParameters application, Typeable application, Typeable controller) => controller -> IO ByteString Source #

Get contents of response

mockActionStatus :: forall application controller. (Controller controller, ContextParameters application, Typeable application, Typeable controller) => controller -> IO Status Source #

Get HTTP status of the controller

responseBody :: Response -> IO ByteString Source #

responseBodyShouldContain :: Response -> Text -> IO () Source #

Asserts that the response body contains the given text.

responseBodyShouldNotContain :: Response -> Text -> IO () Source #

Asserts that the response body does not contain the given text.

responseStatusShouldBe :: Response -> Status -> IO () Source #

Asserts that the response status is equal to the given status.

withUser :: forall user application userId result. (?mocking :: MockContext application, ?applicationContext :: ApplicationContext, ?context :: RequestContext, Serialize userId, HasField "id" user userId, KnownSymbol (GetModelName user)) => user -> ((?context :: RequestContext) => IO result) -> IO result Source #

Set's the current user for the application

Example:

user <- newRecord @User
    |> set #email "marc@digitallyinduced.com"
    |> createRecord

response <- withUser user do
    callAction CreatePostAction

In this example the currentUser will refer to the newly created user during the execution of CreatePostAction

Internally this function overrides the session cookie passed to the application.

idToParam :: forall (table :: Symbol). Show (Id' table) => Id' table -> ByteString Source #

Turns a record id into a value that can be used with callActionWithParams

Example:

Let's say you have a test like this:

 let postId = cs $ show $ post.id

 let params = [ ("postId", postId) ]

You can replace the cs $ show $ with a cleaner idToParam:

 let postId = idToParam (libraryOpening.id)

 let params = [ ("postId", postId) ]