khamusa / rspec-graphql_matchers

Collection of rspec matchers to test your graphQL api schema.
MIT License
145 stars 49 forks source link

Using this with graphql-ruby / rails #38

Closed mathewtrivett closed 2 years ago

mathewtrivett commented 4 years ago

Hi thanks for creating this gem, I'm looking forward to using it. I'm trying to get things set up with:

Ruby: ruby 2.7.0p0 Rails: 6.0.3 Rspec-rails: 4.0.1 Graphql: 1.11.5

I have the following tree type structure for graphql:

app/graphql
├── mutations
│   └── base_mutation.rb
├── tfl_graph_schema.rb
└── types
    ├── base_argument.rb
    ├── base_enum.rb
    ├── base_field.rb
    ├── base_input_object.rb
    ├── base_interface.rb
    ├── base_object.rb
    ├── base_scalar.rb
    ├── base_union.rb
    ├── journey_search_result_type.rb
    ├── journey_type.rb
    ├── mutation_type.rb
    └── query_type.rb

And the following for my specs:

spec
├── graphql
│   └── types
│       └── journey_type_spec.rb
├── rails_helper.rb
├── spec_helper.rb
└── support
    ├── factory_bot.rb
    └── shoulda_matchers.rb

I've create the following spec:

describe JourneyType do
  subject { described_class }

  it { is_expected.to have_field(:arrivalDateTime) }
  it { is_expected.to have_field(:startDateTime) }
  it { is_expected.to have_field(:duration) }
end

But get:

NameError:
  uninitialized constant JourneyType

Do I need to add something to spec_helper.rb to make GraphQL types visible to rspec?

mathewtrivett commented 4 years ago

I actually got this working. It might be helpful to include a little bit extra in the README.md to help others, I'm happy to raise a PR if it would help?

Add the following to spec_helper.rb

require 'rspec/graphql_matchers'
include RSpec::GraphqlMatchers::TypesHelper

I also included in the config config.expose_dsl_globally = true

Then my test file became:

describe Types::JourneyType do
  subject { described_class }

  it { is_expected.to have_field(:arrivalDateTime) }
  it { is_expected.to have_field(:startDateTime) }
  it { is_expected.to have_field(:duration) }
end
khamusa commented 3 years ago

@mathewtrivett first of all, sorry for taking soooooooo long to reply. Things have been crazy and notifications got lost.

So, I'm not sure why it didn't work for you in the first place. Looking at the error message, it looks like it has nothing to do with the gem. It just says a constant was not found. JourneyType is a constant as any other, and if your spec file can't find it... :boom:

From your proposed solution I guess your constant is not defined at the root of the module namespace, but under Types. In that case, what solved your issue was using the fully qualified constant name Types::JourneyType.

The module RSpec::GraphqlMatchers::TypesHelper is actually deprecated. I invite you to remove it and try running the specs again. It was a helper created for much older versions of the graphql gem (before the class / module interface for defining types was introduced), and it has been kept on the codebase for compatibility reasons only. I guess it's safe to remove it on the next release...

tobmatth commented 3 years ago

@mathewtrivett

Try to remove

require 'rspec/graphql_matchers'
include RSpec::GraphqlMatchers::TypesHelper

from your spec_helper.rb and make sure you've got --require rails_helper in your .rspec instead of --require spec_helper.

HTH Tobi