hspec-snap-0.1.0.0: A library for testing with Hspec and the Snap Web Framework

Safe HaskellNone

Test.Hspec.Snap

Contents

Synopsis

Running blocks of hspec-snap tests

snap :: Handler b b () -> SnapletInit b b -> SpecWith (SnapHspecState b) -> SpecSource

The way to run a block of SnapHspecM tests within an hspec test suite. This takes both the top level handler (usually `route routes`, where routes are all the routes for your site) and the site initializer (often named app), and a block of tests. A test suite can have multiple calls to snap, though each one will cause the site initializer to run, which is often a slow operation (and will slow down test suites).

Core data types

data TestResponse Source

The result of making requests against your application. Most assertions act against these types (for example, should200, shouldHaveSelector, etc).

type SnapHspecM b = StateT (SnapHspecState b) IOSource

The main monad that tests run inside of. This allows both access to the application (via requests and eval) and to running assertions (like should404 or shouldHaveText).

General Hspec helpers

afterAll :: IO () -> SpecWith a -> SpecWith aSource

Runs a given action once after all the tests in the given block have run.

Requests

get :: Text -> SnapHspecM b TestResponseSource

Runs a GET request.

get' :: Text -> Params -> SnapHspecM b TestResponseSource

Runs a GET request, with a set of parameters.

post :: Text -> Params -> SnapHspecM b TestResponseSource

Creates a new POST request, with a set of parameters.

paramsSource

Arguments

:: [(ByteString, ByteString)]

Pairs of parameter and value.

-> Params 

A helper to construct parameters.

Evaluating application code

eval :: Handler b b a -> SnapHspecM b aSource

Runs an arbitrary stateful action from your application.

Unit test assertions

shouldEqual :: (Show a, Eq a) => a -> a -> SnapHspecM b ()Source

Asserts that two values are equal.

shouldNotEqual :: (Show a, Eq a) => a -> a -> SnapHspecM b ()Source

Asserts that two values are not equal.

shouldBeTrue :: Bool -> SnapHspecM b ()Source

Asserts that the value is True.

shouldNotBeTrue :: Bool -> SnapHspecM b ()Source

Asserts that the value is not True (otherwise known as False).

Response assertions

should200 :: TestResponse -> SnapHspecM b ()Source

Asserts that the response is a success (either Html, or Other with status 200).

shouldNot200 :: TestResponse -> SnapHspecM b ()Source

Asserts that the response is not a normal 200.

should404 :: TestResponse -> SnapHspecM b ()Source

Asserts that the response is a NotFound.

shouldNot404 :: TestResponse -> SnapHspecM b ()Source

Asserts that the response is not a NotFound.

should300 :: TestResponse -> SnapHspecM b ()Source

Asserts that the response is a redirect.

shouldNot300 :: TestResponse -> SnapHspecM b ()Source

Asserts that the response is not a redirect.

should300To :: Text -> TestResponse -> SnapHspecM b ()Source

Asserts that the response is a redirect, and thet the url it redirects to starts with the given path.

shouldNot300To :: Text -> TestResponse -> SnapHspecM b ()Source

Asserts that the response is not a redirect to a given path. Note that it can still be a redirect for this assertion to succeed, the path it redirects to just can't start with the given path.

shouldHaveSelector :: TestResponse -> Text -> SnapHspecM b ()Source

Assert that a response (which should be Html) has a given selector.

shouldNotHaveSelector :: TestResponse -> Text -> SnapHspecM b ()Source

Assert that a response (which should be Html) doesn't have a given selector.

shouldHaveText :: TestResponse -> Text -> SnapHspecM b ()Source

Asserts that the response (which should be Html) contains the given text.

shouldNotHaveText :: TestResponse -> Text -> SnapHspecM b ()Source

Asserts that the response (which should be Html) does not contain the given text.

Form tests

data FormExpectations a Source

A data type for tests against forms.

Constructors

Value a

The value the form should take (and should be valid)

ErrorPaths [Text]

The error paths that should be populated

formSource

Arguments

:: (Eq a, Show a) 
=> FormExpectations a

If the form should succeed, Value a is what it should produce. If failing, ErrorPaths should be all the errors that are triggered.

-> Form Text (Handler b b) a

The form to run

-> Map Text Text

The parameters to pass

-> SnapHspecM b () 

Tests against digestive-functors forms.

Internal types and helpers

data SnapHspecState b Source

Internal state used to share site initialization across tests, and to propogate failures.

Constructors

SnapHspecState Result (Handler b b ()) (Snaplet b) (InitializerState b) 

Instances

setResult :: Result -> SnapHspecM b ()Source

Records a test Success or Fail. Only the first Fail will be recorded (and will cause the whole block to Fail).

runRequest :: RequestBuilder IO () -> SnapHspecM b TestResponseSource

Runs a request (built with helpers from Snap.Test), resulting in a response.

runHandlerSafe :: RequestBuilder IO () -> Handler b b v -> Snaplet b -> InitializerState b -> IO (Either Text Response)Source

Runs a request against a given handler (often the whole site), with the given state. Returns any triggered exception, or the response.

evalHandlerSafe :: Handler b b v -> Snaplet b -> InitializerState b -> IO (Either Text v)Source

Evaluates a given handler with the given state. Returns any triggered exception, or the value produced.