PhoenixSwagger.SchemaTest (phoenix_swagger v0.8.3) View Source

Testing helper module that makes it convenient to assert that phoenix controller responses conform to a swagger spec.

Example

use YourApp.ConnCase
use PhoenixSwagger.SchemaTest "priv/static/swagger.json"

test "Get a user by ID", %{conn: conn, swagger_schema: schema} do
  user = Repo.insert! struct(User, @valid_attrs)
  response =
    conn
    |> get(user_path(conn, :show, user))
    |> validate_resp_schema(schema, "UserResponse")
    |> json_response(200)

  assert response["data"]["id"] == user.id
end

Errors will be output with the json-path of the location in the response body for the error:

Response JSON does not conform to swagger schema from #/definitions/UserResponse.
At #/data/email: Expected "foobaz" to be an email address.
{
  "data": {
    "name": "Yu Ser",
    "id": 141,
    "email": "foobaz"
  }
}

Link to this section Summary

Functions

Given a swagger file path, defines an ExUnit setup_all block that will add the resolved swagger schema to the ExUnit context.

Validates a response body against a swagger schema.

Link to this section Functions

Link to this macro

__using__(swagger_file)

View Source (macro)

Given a swagger file path, defines an ExUnit setup_all block that will add the resolved swagger schema to the ExUnit context.

Link to this function

validate_resp_schema(conn, swagger_schema, model_name)

View Source

Validates a response body against a swagger schema.

Example

use MyApp.ConnCase
use PhoenixSwagger.SchemaTest "priv/static/swagger.json"

test "get user by ID", %{conn: conn, swagger_schema: schema} do
  response =
    conn
    |> get(user_path(conn, :show, 123))
    |> validate_resp_schema(schema, "User")
    |> json_response(200)

  assert response["data"]["id"] == 123
end