Assertions v0.18.1 Assertions.Absinthe View Source

Helpful assertions for testing Absinthe applications.

This module contains some functions that make it much simpler and safer to test Absinthe applications. Probably the most common issue that is seen in Absinthe applications is untested resolver functions and fields, and it's nearly impossible to tell using just code coverage which fields are tested or not.

These functions make it trivially easy to generate very large, comprehensive queries for our types in Absinthe that will resolve every field in that type (and any number of subtypes as well to a given level of depth), and by default makes it so that we're either testing equality of the response or testing a pattern match on the response.

While many of these functions explicitly take the schema as the first argument, if you want to simplify things you can use the provided ExUnit case template like so:

use Assertions.AbsintheCase, async: true, schema: MyApp.Schema

and then all functions in this module will not need the schema passed explicitly into it.

Link to this section Summary

Functions

Assert that the response for sending document equals expected_response

Assert that the response for sending document matches expr

Returns a document containing the fields in a type and any sub-types down to a limited depth of nesting (default 3)

Returns all fields in a type and any sub-types down to a limited depth of nesting (default 3)

Link to this section Functions

Link to this function

assert_response_equals(schema, document, expected_response, options) View Source
assert_response_equals(module(), String.t(), map(), Keyword.t()) ::
  :ok | no_return()

Assert that the response for sending document equals expected_response.

This is helpful when you want to exhaustively test something by asserting equality on every field in the response.

## Example

##> query = "{ user { #{document_for(:user, 2} } }" ##> expected = %{"user" => %{"name" => "Bob", "posts" => [%{"title" => "A post"}]}} ##> assert_response_equals(query, expected)

Link to this macro

assert_response_matches(schema, document, options, list) View Source (macro)
assert_response_matches(module(), String.t(), Keyword.t(), Macro.expr()) ::
  :ok | no_return()

Assert that the response for sending document matches expr.

This is helpful when you want to test some but not all fields in the returned response, or would like to break up your assertions by binding variables in the body of the match and then making separate assertions further down in your test.

## Example

##> query = "{ user { #{documentfor(:user, 2} } }" ##> assert_response_matches(query) do ##> %{"user" => %{"name" => "B" <> , "posts" => posts}} ##> end ##> assert length(posts) == 1

Link to this function

document_for(schema, type, nesting, overrides) View Source
document_for(module(), atom(), non_neg_integer(), Keyword.t()) :: String.t()

Returns a document containing the fields in a type and any sub-types down to a limited depth of nesting (default 3).

This is helpful for generating a document to use for testing your GraphQL API. This function will always return all fields in the given type, ensuring that there aren't any accidental fields with resolver functions that aren't tested in at least some fashion.

## Example

#=> document_for(:user, 2) "\"" name age posts {

title
subtitle

} comments {

body

} "\""

Link to this function

fields_for(schema, type, nesting) View Source
fields_for(module(), atom(), non_neg_integer()) :: [fields] | atom()
when fields: atom() | {atom(), [fields]}

Returns all fields in a type and any sub-types down to a limited depth of nesting (default 3).

This is helpful for converting a struct or map into an expected response that is a bare map and which can be used in some of the other assertions below.