Oaskit.Test (oaskit v0.3.0)

View Source

Provides the valid_response/3 test helper to validate API responses in your ExUnit tests.

Summary

Functions

Validates that the given conn bears a response that is valid with regard to the OpenAPI operation that served it, and returns the response body.

Functions

valid_response(spec_module, conn, status)

Validates that the given conn bears a response that is valid with regard to the OpenAPI operation that served it, and returns the response body.

Responses returned with a JSON based content-type like "application/json" or "application/vnd.api+json" will be decoded.

It is encouraged to wrap this function with a custom helper, typically in your MyAppWeb.ConnCase test helper:

defmodule MyAppWeb.ConnCase do

  # ...

  # You can wrap the helper function this way so you do not have to pass
  # The spec module in every call:
  def valid_response(conn, status) do
    Oaskit.Test.valid_response(MyAppWeb.OpenAPISpec, conn, status)
  end
end

Then use the helper in your tests:

test "should return the user info", %{conn: conn} do
  %{id: id} = user_fixture(username: "joe", roles: ["admin"])
  conn = get(conn, ~p"/api/users/#{id}")

  assert %{
    "username" => "joe",
    "roles" => ["admin"]
  } = valid_response(conn, 200)
end