View Source AshJsonApi.Test (ash_json_api v1.4.16)

Utilities for testing AshJsonApi.

Making Requests

The request testing functions get/patch/post/delete all support the following options

  • :status: Asserts that the response has the provided status after making the request
  • :router: The corresponding JsonApiRouter to go through. Can be set statically in config, see below for more.
  • :actor: Sets the provided actor as the actor for the request
  • :tenant: Sets the provided tenant as the tenant for the request

A standard test would look like this:

test "can list posts", %{current_user: current_user} do
  Domain
  # GET /posts
  # assert resp.status == 200
  |> get("/posts", status: 200, actor: current_user, router: MyAppWeb.JsonApiRouter)
  # pattern match on the data key of the response
  |> assert_data_matches([
    %{
      "attributes" => %{
        "name" => "foo"
      }
    }
  ])
end

Summary

Functions

Assert that the response body's "data" equals an exact value

Assert that the response body's "data" matches a pattern

Asserts that an error is in the response where each key present in the provided map has the same value in the error.

Assert that the given function returns true for at least one included record

Sends a DELETE request to the given path. See the module docs for more.

Sends a GET request to the given path. See the module docs for more.

Sends a multipart POST request to the given path. See the module docs for more.

Sends a PATCH request to the given path. See the module docs for more.

Sends a POST request to the given path. See the module docs for more.

Refute that the given function returns true for at least one included record

Functions

Link to this macro

assert_data_equals(conn, expected_data)

View Source (macro)

Assert that the response body's "data" equals an exact value

Link to this macro

assert_data_matches(conn, data_pattern)

View Source (macro)

Assert that the response body's "data" matches a pattern

Link to this macro

assert_has_error(conn, fields)

View Source (macro)

Asserts that an error is in the response where each key present in the provided map has the same value in the error.

Example

Domain
|> delete("/posts/1", status: 404)
|> assert_has_error(%{
  "code" => "not_found",
  "detail" => "No post record found with `id: 1`",
  "title" => "Entity Not Found"
})
Link to this macro

assert_has_matching_include(conn, function)

View Source (macro)

Assert that the given function returns true for at least one included record

Example

Domain |> get("/posts/#{post.id}/?include=author", status: 200) |> assert_has_matching_include(fn %{"type" => "author", "id" => ^author_id} ->

true

_ ->

false

end)

Link to this function

delete(domain, path, opts \\ [])

View Source

Sends a DELETE request to the given path. See the module docs for more.

Link to this function

get(domain, path, opts \\ [])

View Source

Sends a GET request to the given path. See the module docs for more.

Link to this function

multipart_post(domain, path, body, opts \\ [])

View Source

Sends a multipart POST request to the given path. See the module docs for more.

Link to this function

patch(domain, path, body, opts \\ [])

View Source

Sends a PATCH request to the given path. See the module docs for more.

Link to this function

post(domain, path, body, opts \\ [])

View Source

Sends a POST request to the given path. See the module docs for more.

Link to this macro

refute_has_matching_include(conn, function)

View Source (macro)

Refute that the given function returns true for at least one included record

Example

Domain |> get("/posts/#{post.id}/?include=author", status: 200) |> refute_has_matching_include(fn %{"type" => "author", "id" => ^author_id} ->

true

_ ->

false

end)