# `Oaskit.Test`
[🔗](https://github.com/lud/oaskit/blob/v0.17.1/lib/oaskit/test.ex#L1)

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

Response validators are built on the first call and then cached. To build
them before the test suite starts, add this line to your `test/test_helper.exs`
file, after `ExUnit.start()`:

    :ok = Oaskit.warmup_spec_cache(MyAppWeb.OpenAPISpec, responses: true)

# `valid_response`

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

---

*Consult [api-reference.md](api-reference.md) for complete listing*
