[Recommended] A String representation of a type: "String!", "Int!", "[String]!"
(note the exclamation mark at the end, as required by the GraphQL specs.
For objects defined with the legacy #define api, testing :property, :hash_key and metadata is also possible by chaining .with_property, .with_hash_key and .with_metadata. For example:
describePostTypedosubject{described_class}it{is_expected.tohave_field(:id).of_type(!types.ID)}it{is_expected.tohave_field(:comments).of_type("[String!]!")}it{is_expected.tohave_field(:isPublished).of_type("Boolean")}# Check a field is deprecatedit{is_expected.tohave_field(:published).with_deprecation_reason}it{is_expected.tohave_field(:published).with_deprecation_reason('Use isPublished instead')}it{is_expected.not_tohave_field(:published).with_deprecation_reason('Wrong reason')}it{is_expected.not_tohave_field(:isPublished).with_deprecation_reason}# The gem automatically converts field names to CamelCase, so this will# pass even though the field was defined as field :isPublishedit{is_expected.tohave_field(:is_published).of_type("Boolean")}end
2) Test a specific field type with be_of_type matcher:
4) Test the arguments accepted by a field with accept_argument matcher:
describePostTypedodescribe'subposts'dosubject{PostType.fields['subposts']}it'accepts a filter and an id argument, of types String and ID'doexpect(subject).toaccept_argument(:filter).of_type('String')expect(subject).toaccept_argument(:id).of_type('ID')endit{is_expected.not_toaccept_argument(:weirdo)}# The gem automatically converts argument names to CamelCase, so this will# pass even though the argument was defined as :isPublishedit{is_expected.toaccept_argument(:is_published).of_type("Boolean")}endend
5) Test an object's interface implementations:
describePostTypedosubject{described_class}it'implements interface Node'doexpect(subject).toimplement('Node')end# Accepts arguments as an array and type objects directlyit{is_expected.toimplement(GraphQL::Relay::Node.interface)}it{is_expected.not_toimplement('OtherInterface')}end
6) Using camelize: false on field names
By default the graphql gem camelizes field names. This gem deals with it transparently:
classObjectMessingWithCamelsAndSnakesType < GraphQL::Schema::Objectgraphql_name'ObjectMessingWithCamelsAndSnakes'implementsGraphQL::Relay::Node.interfacefield:me_gusta_los_camellos,ID,null: false# note the camelize: falsefield:prefiero_serpientes,ID,null: false,camelize: falseend
The following specs demonstrate the current behavior of the gem regarding fields:
describeObjectMessingWithCamelsAndSnakesTypedosubject{described_class}# For a field name that was automatically camelized, you can add expectations# against both versions and we handle it transparently:it{is_expected.tohave_a_field(:meGustaLosCamellos)}it{is_expected.tohave_a_field(:me_gusta_los_camellos)}# However, when using camelize: false, you have to use the exact case of the field definition:it{is_expected.tohave_a_field(:prefiero_serpientes)}it{is_expected.not_tohave_a_field(:prefieroSerpientes)}# Note we're using `not_to`end
This behaviour is currently active only on field name matching. PRs are welcome to
reproduce it to arguments as well.
TODO
New matchers!
Contributing
Send Bug reports, suggestions or any general
question through the Issue tracker.
Think of another matcher that could be useful? This is the place to ask, or...
Pull requests are welcome through the usual procedure: fork the project,
commit your changes and open the PR.
This project is intended to be a safe, welcoming space for collaboration, and
contributors are expected to adhere to the
Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the
MIT License.
请发表评论