Short description and motivation.
For testing both queries and types you'll want to set the subject to described_class, as Rspec will automatically try to create an instance of the class, which is not possible with the Stardust GraphQL types and queries. Each matcher takes the subject and uses the appropriate property on the subject for its testing purposes.
To set the subject, simply use this snippet in the context of your testing of the types and queries.
subject {described_class}
For other tests of the class, you can wrap them in a context or describe and use a different subject if needed.
For testing queries, you'll want to test that the query has:
- the proper return type
- for all arguments it should take
- if it can return null
- what resolve does
Return Type
it{should return_type(:symbol_of_your_return_type)}
Arguments
it{should have_argument({name: :id, type: :id, named_args: {required: true}})}
Nullability
The class can return null
it{should be_able_to_return_null}
The class cannot return null
it{should_not be_able_to_return_null}
resolve
There are no matchers for resolve at the moment. What resolve does is what makes the query unique, so while there may be an opportunity to create matchers for common things, we don't have any yet. You should unit test resolve as you would any other method on a class.
Nullability
The class can return null
it{should be_able_to_return_null}
The class cannot return null
it{should_not be_able_to_return_null}
For types, we want to test that the proper fields are present. Below is a full example.
require "rails_helper"
RSpec.describe Stardust::GraphQL::Collector.types[:line_item] do
subject {described_class}
describe "fields" do
it{should have_field(:id,:id, null:false)}
it{should have_field(:details,:details, null:false)}
it{should have_field(:product,:product, null:true)}
end
end
In this example for the type line_item set the subject as described above, then we put the fields in a describe block. From there we test each field to make sure it is present with the right name,data type, and nullability.
In the example below, we want to make sure that the type has a field named id, with the type of id, and it cannot be null.
it{should have_field(:id,:id, null:false)}
In the example below, we want to make sure that the type has a field named name, with the type of string and it can be null.
it{should have_field(:name,:string, null:true)}
In the example below, we want to make sure that the type has a field named line_items, that returns an array of the type line_item and it cannot be null.
it{should have_field(:line_items,[:line_item], null:false)}
Both mutations
and queries
can be tested with the authorization matchers.
Set the subject
to decribed_class
, then use a combination of the following three matchers to verify that your class is performing authorization as it should.
factory_bot factory names should be passed in.
describe ".authorized?" do
subject {described_class}
it{should allow_anonymous(false)}
it{should allow_access_to [:dealer_user]}
it{should deny_access_to [:admin_user,:shop_user]}
end
Add this line to your application's Gemfile, within the test group:
group :test do
gem 'stardust-rspec', git: '[email protected]:hatch-software/startdust-rspec.git'
end
And then execute:
$ bundle
Contribution directions go here.
The gem is available as open source under the terms of the MIT License.