PhoenixIntegration.Assertions (phoenix_integration v0.9.2) View Source

Functions to assert/refute the response content of a conn without interrupting the chain of actions in an integration test.

Each function takes a conn and a set of conditions to test. Each condition is tested and, if they all pass, the function returns the passed-in conn unchanged. If any condition fails, the function raises an appropriate error.

This is intended to be used in a (possibly long) chain of piped functions that exercises a set of functionality in your application.

Example

test "Basic page flow", %{conn: conn} do
  # get the root index page
  get( conn, page_path(conn, :index) )
  # click/follow through the various about pages
  |> follow_link( "About Us" )
  |> assert_response( status: 200, path: about_path(conn, :index) )
  |> follow_link( "Contact" )
  |> assert_response( content_type: "text/html" )
  |> follow_link( "Privacy" )
  |> assert_response( html: "Privacy Policy" )
  |> follow_link( "Home" )
  |> assert_response( status: 200, path: page_path(conn, :index) )
end

Link to this section Summary

Functions

Asserts a set of conditions against the response fields of a conn. Returns the conn on success so that it can be used in the next integration call.

Refutes a set of conditions for the response fields in a conn. Returns the conn on success so that it can be used in the next integration call.

Link to this section Functions

Link to this function

assert_response(conn, conditions)

View Source

Asserts a set of conditions against the response fields of a conn. Returns the conn on success so that it can be used in the next integration call.

Parameters

  • conn should be a conn returned from a previous request should point to the path being redirected to.
  • conditions a list of conditions to test against. Conditions can include:
    • :status checks that conn.status equals the given numeric value
    • :content_type the conn's content-type header should contain the given text. Typical values are "text/html" or "application/json"
    • :body conn.resp_body should contain the given text. Does not check the content_type.
    • :html checks that content_type is html, then looks for the given text in the body.
    • :json checks that content_type is json, then checks that the json data equals the given map.
    • :path the route rendered into the conn must equal the given path (or uri).
    • :uri same as :path
    • :redirect checks that conn.status is 302 and that the path in the "location" redirect header equals the given path.
    • :to same as :redirect
    • :assigns checks that conn.assigns contains the given values, which could be in the form of %{key => value} or [{key, value}]
    • :value checks that the value returned by a callback (in the form fn(conn)) is truthy

Conditions can be used multiple times within a single call to assert_response. This can be useful to look for multiple text strings in the body.

Example

# test a rendered page
assert_response( conn,
  status:   200,
  path:     page_path(conn, :index),
  html:     "Some Content",
  html:     "More Content",
  assigns:  %{current_user_id: user.id}
)

# test a redirection
assert_response( conn, to: page_path(conn, :index) )

# test a callback value
assert_response( conn, value: fn(conn) ->
  Guardian.Plug.current_resource(conn)
end)
Link to this function

refute_response(conn, conditions)

View Source

Refutes a set of conditions for the response fields in a conn. Returns the conn on success so that it can be used in the next integration call.

Parameters

  • conn should be a conn returned from a previous request should point to the path being redirected to.
  • conditions a list of conditions to test against. Conditions can include:
    • :status checks that conn.status is not the given numeric value
    • :content_type the conn's content-type header should not contain the given text. Typical values are "text/html" or "applicaiton/json"
    • :body conn.resp_body should not contain the given text. Does not check the content_type.
    • :html checks if content_type is html. If it is, it then checks that the given text is not in the body.
    • :json checks if content_type is json, then checks that the json data does not equal the given map.
    • :path the route rendered into the conn must not equal the given path (or uri).
    • :uri same as :path
    • :redirect checks if conn.status is 302. If it is, then checks that the path in the "location" redirect header is not the given path.
    • :to same as :redirect
    • :assigns checks that conn.assigns does not contain the given values, which could be in the form of %{key: value} or [{:key, value}]
    • :value checks that the value returned by a callback (in the form fn(conn)) is false or nil

refute_response is often used in conjuntion with assert_response to form a complete condition check.

Example

# test a rendered page
follow_path( conn, page_path(conn, :index) )
|> assert_response(
    status: 200,
    path:   page_path(conn, :index)
    html:   "Good Content"
  )
|> refute_response( body: "Invalid Content" )