View Source Assertions.Absinthe (Assertions v0.20.1)
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.
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
).
Functions
assert_response_equals(schema, document, expected_response, options)
View SourceAssert 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)
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 { #{document_for(:user, 2)} } }"
assert_response_matches(query) do
%{"user" => %{"name" => "B" <> _, "posts" => posts}}
end
assert length(posts) == 1
@spec 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
}
"""
@spec 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.