AshJsonApi.Test (ash_json_api v1.4.30)

View Source

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

assert_data_equals(conn, expected_data)

(macro)

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

assert_data_matches(conn, data_pattern)

(macro)

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

assert_has_error(conn, fields)

(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"
})

assert_has_matching_include(conn, function)

(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)

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

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

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

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

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

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

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

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

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

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

refute_has_matching_include(conn, function)

(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)