snap-testing-0.4.1.1: A library for BDD-style testing with the Snap Web Framework

Safe HaskellNone

Snap.Test.BDD

Contents

Synopsis

Types

type SnapTesting b a = StateT (Handler b b (), SnapletInit b b, OutputStream TestLog) IO aSource

The main type for this library, where b is your application state, often called App. This is a State monad on top of IO, where the State carries your application (or, more specifically, a top-level handler), and stream of test results to be reported as passing or failing.

type TestRequest = RequestBuilder IO ()Source

TestRequests are created with get and post.

data TestLog Source

TestLog is what is streamed to report generators. It is a flatten tree structure.

Instances

Configuration

Running tests

runSnapTestsSource

Arguments

:: SnapTestingConfig

Configuration for test runner

-> Handler b b ()

Site that requests are run against (often route routes, where routes are your sites routes).

-> SnapletInit b b

Site initializer

-> SnapTesting b ()

Block of tests

-> IO () 

Run a set of tests, putting the results through the specified report generators

consoleReport :: InputStream TestLog -> IO ()Source

Prints test results to the console. For example:

 /auth/new_user
  success PASSED
  creates a new account PASSED

linuxDesktopReport :: InputStream TestLog -> IO ()Source

Sends the test results to desktop notifications on linux. Prints how many tests passed and failed.

Labeling

nameSource

Arguments

:: Text

Name of block

-> SnapTesting b ()

Block of tests

-> SnapTesting b () 

Labels a block of tests with a descriptive name, to be used in report generation.

Creating Requests

getSource

Arguments

:: ByteString

The url to request.

-> TestRequest 

Creates a new GET request.

get'Source

Arguments

:: ByteString

The url to request.

-> Map ByteString [ByteString]

The parameters to send.

-> TestRequest 

Creates a new GET request, with query parameters.

postSource

Arguments

:: ByteString

The url to request.

-> Map ByteString [ByteString]

The parameters to send.

-> TestRequest 

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

paramsSource

Arguments

:: [(ByteString, ByteString)]

Pairs of parameter and value.

-> Map ByteString [ByteString] 

A helper to construct parameters.

Request predicates

succeeds :: TestRequest -> SnapTesting b ()Source

Checks that the given request results in a success (200) code.

notfound :: TestRequest -> SnapTesting b ()Source

Checks that the given request results in a not found (404) code.

redirects :: TestRequest -> SnapTesting b ()Source

Checks that the given request results in a redirect (3**) code.

redirectstoSource

Arguments

:: TestRequest

Request to run

-> Text

URL it should redirect to

-> SnapTesting b () 

Checks that the given request results in a redirect to a specific url.

changesSource

Arguments

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

Change function

-> Handler b b a

Monadic value

-> TestRequest

Request to run.

-> SnapTesting b () 

Checks that the monadic value given changes by the function specified after the request is run.

For example, if you wanted to make sure that account creation was creating new accounts:

 changes (+1) countAccounts (post "/auth/new_user" $ params
                             [ ("new_user.name", "Jane")
                             , ("new_user.email", "jdoe@c.com")
                             , ("new_user.password", "foobar")])

changes'Source

Arguments

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

Change function

-> Handler b b a

Monadic value

-> SnapTesting b c

Block of tests to run

-> SnapTesting b () 

A more general variant of changes that allows an arbitrary block instead of a request.

containsSource

Arguments

:: TestRequest

Request to run

-> Text

Text that body should contain

-> SnapTesting b () 

Checks that the response body of a given request contains some text.

notcontainsSource

Arguments

:: TestRequest

Request to run

-> Text

Text that body should not contain

-> SnapTesting b () 

Checks that the response body of a given request does not contain some 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

-> SnapTesting b () 

Test against digestive-functors forms.

Stateful unit tests

equalsSource

Arguments

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

Value to compare against

-> Handler b b a

Handler that should evaluate to the same thing

-> SnapTesting b () 

Checks that the handler evaluates to the given value.

Pure unit tests

assert :: Bool -> SnapTesting b ()Source

Helper to bring the results of other tests into the test suite.

Run actions after block

cleanupSource

Arguments

:: Handler b b ()

Action to run after tests

-> SnapTesting b ()

Tests to run

-> SnapTesting b () 

Runs an action after a block of tests, usually used to remove database state.

Evaluate arbitrary action

evalSource

Arguments

:: Handler b b a

Action to evaluate

-> SnapTesting b a 

Evaluate arbitrary actions

Create helpers

modifySiteSource

Arguments

:: (Handler b b () -> Handler b b ())

Site modification function

-> SnapTesting b a

Tests to run

-> SnapTesting b a 

Given a site to site function (like, generating a random user and logging in), run the given block of test with the modified state.

Integrate with QuickCheck

quickCheck :: Testable prop => prop -> SnapTesting b ()Source

Allows you to run a quickcheck test. All 100 test passing counts as a pass, any failure a failure. Currently the reporting is really bad (you don't see what the failing example is).