Skip to content

Latest commit

 

History

History
48 lines (35 loc) · 1.56 KB

add_matcher.md

File metadata and controls

48 lines (35 loc) · 1.56 KB

Adding RSpec Matchers

RSpec::GraphQLResponse provides a thin layer around RSpec's native matcher syntax to ensure the matchers you are adding will be included correctly.

This is usefurl whether you are developing new matchers to be included in this gem directly, or adding them to your application for your specific needs.

RSpec::GraphQLResponse.add_matcher

To add a new matcher, call RSpec::GraphQLResponse.add_matcher.

RSpec::GraphQLResponse.add_matcher do |some_option|
  # ... this is a standard RSpec `matcher` block
  # where you can do anything you would do in RSpec

  match :foo_bar do
    # do your validation here
  end
end

This call is only a wrapper around RSpec's matcher. For information on how to build your matcher, see the RSpec project documentation.

Combined with a Custom Validator

RSpec::GraphQLResponse provides a means of keeping your matcher code clean while also making it easy to unit test the logic and messages supplied from the matcher, using RSpec::GraphQLResponse.add_validator.

Once a validator has been created, it can be called from a matcher.

RSpec::GraphQLResponse.add_matcher :foo_bar do
  match do |response|
    have_errors = RSpec::GraphQLResponse.validator(:foo_bar)
    @result = have_errors.validate
  end

  failure_message do |response|
    @result.reason
  end
end

For information on creating a valdator, see the add validators documentation.