| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
IHP.Test.Mocking
Synopsis
- type ContextParameters application = (?request :: Request, ?respond :: Respond, ?modelContext :: ModelContext, ?application :: application, InitControllerContext application, ?mocking :: MockContext application)
- data MockContext application = InitControllerContext application => MockContext {
- modelContext :: ModelContext
- frameworkConfig :: FrameworkConfig
- mockRequest :: Request
- mockRespond :: Respond
- application :: application
- runTestMiddlewares :: FrameworkConfig -> ModelContext -> Request -> IO Request
- mockContextNoDatabase :: InitControllerContext application => application -> ConfigBuilder -> IO (MockContext application)
- withMockContext :: InitControllerContext application => application -> ConfigBuilder -> (MockContext application -> IO a) -> IO a
- initTestApplication :: FrontController RootApplication => MockContext application -> IO Application
- withMockContextAndApp :: (InitControllerContext application, FrontController RootApplication) => application -> ConfigBuilder -> ((MockContext application, Application) -> IO a) -> IO a
- withContextAndApp :: (ContextParameters application => Application -> IO a) -> (MockContext application, Application) -> IO a
- withContext :: (ContextParameters application => IO a) -> MockContext application -> IO a
- setupWithContext :: (ContextParameters application => IO a) -> MockContext application -> IO (MockContext application)
- callAction :: forall application controller. (Controller controller, ContextParameters application, Typeable application, Typeable controller) => controller -> IO Response
- callActionWithParams :: forall application controller. (Controller controller, ContextParameters application, Typeable application, Typeable controller) => controller -> [Param] -> IO Response
- callJob :: (ContextParameters application, Typeable application, Job job) => job -> IO ()
- mockAction :: forall application controller. (Controller controller, ContextParameters application, Typeable application, Typeable controller) => controller -> IO Response
- mockActionResponse :: forall application controller. (Controller controller, ContextParameters application, Typeable application, Typeable controller) => controller -> IO ByteString
- mockActionStatus :: forall application controller. (Controller controller, ContextParameters application, Typeable application, Typeable controller) => controller -> IO Status
- responseBody :: Response -> IO ByteString
- responseBodyShouldContain :: Response -> Text -> IO ()
- responseBodyShouldNotContain :: Response -> Text -> IO ()
- responseStatusShouldBe :: Response -> Status -> IO ()
- withUser :: forall user application userId result. (?mocking :: MockContext application, ?request :: Request, ?respond :: Respond, Serialize userId, HasField "id" user userId, KnownSymbol (GetModelName user)) => user -> ((?request :: Request, ?respond :: Respond) => IO result) -> IO result
- idToParam :: forall (table :: Symbol). Show (Id' table) => Id' table -> ByteString
Documentation
type ContextParameters application = (?request :: Request, ?respond :: Respond, ?modelContext :: ModelContext, ?application :: application, InitControllerContext application, ?mocking :: MockContext application) Source #
data MockContext application Source #
Constructors
| InitControllerContext application => MockContext | |
Fields
| |
runTestMiddlewares :: FrameworkConfig -> ModelContext -> Request -> IO Request Source #
Run a request through the test middleware stack. This applies the same middlewares that IHP.Server uses (with PGListener disabled). Used for initial setup only - actual request params are handled in callActionWithParams.
mockContextNoDatabase :: InitControllerContext application => application -> ConfigBuilder -> IO (MockContext application) Source #
Deprecated: Use withMockContext instead for bracket-style resource management
withMockContext :: InitControllerContext application => application -> ConfigBuilder -> (MockContext application -> IO a) -> IO a Source #
Bracket-style mock context creation with proper resource cleanup.
Uses withModelContext to ensure the database pool is released when done.
Prefer this over mockContextNoDatabase.
Example: Use with hspec's aroundAll:
tests :: Spec
tests = aroundAll (withMockContext WebApplication config) do
it "should work" $ withContext do
...initTestApplication :: FrontController RootApplication => MockContext application -> IO Application Source #
Build a WAI Application from a MockContext for use with runSession.
withMockContextAndApp :: (InitControllerContext application, FrontController RootApplication) => application -> ConfigBuilder -> ((MockContext application, Application) -> IO a) -> IO a Source #
Combines withMockContext and initTestApplication into a single bracket.
Example: Use with hspec's aroundAll:
tests :: Spec
tests = aroundAll (withMockContextAndApp WebApplication config) do
it "should work" $ withContextAndApp \application -> do
runSession (testGet "/foo") application >>= assertSuccess "bar"withContextAndApp :: (ContextParameters application => Application -> IO a) -> (MockContext application, Application) -> IO a Source #
Like withContext but for specs using withMockContextAndApp.
The WAI Application is passed to the callback.
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, ?request :: Request, ?respond :: Respond, Serialize userId, HasField "id" user userId, KnownSymbol (GetModelName user)) => user -> ((?request :: Request, ?respond :: Respond) => 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 CreatePostActionIn 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) ]