Sha256: 05e31b925da664df6ef46a6cbc8e4c28adb7ce2feda4a39ac75597c23cf6c24b

Contents?: true

Size: 827 Bytes

Versions: 5

Compression:

Stored size: 827 Bytes

Contents

# frozen_string_literal: true

require_relative 'base_matcher'

module RSpec
  module GraphqlMatchers
    class BeOfType < BaseMatcher
      attr_reader :sample, :expected

      def initialize(expected)
        @expected = expected
      end

      def matches?(actual_sample)
        @sample = to_graphql(actual_sample)
        sample.respond_to?(:type) && types_match?(sample.type, @expected)
      end

      def failure_message
        "expected field '#{member_name(sample)}' to be of type '#{expected}', " \
        "but it was '#{type_name(sample.type)}'"
      end

      def description
        "be of type '#{expected}'"
      end

      private

      def to_graphql(field_sample)
        return field_sample unless field_sample.respond_to?(:to_graphql)

        field_sample.to_graphql
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rspec-graphql_matchers-1.3.1 lib/rspec/graphql_matchers/be_of_type.rb
rspec-graphql_matchers-1.3.0 lib/rspec/graphql_matchers/be_of_type.rb
rspec-graphql_matchers-1.2.1 lib/rspec/graphql_matchers/be_of_type.rb
rspec-graphql_matchers-1.2 lib/rspec/graphql_matchers/be_of_type.rb
rspec-graphql_matchers-1.1 lib/rspec/graphql_matchers/be_of_type.rb