exAspArk / graphql-guard

Simple authorization gem for GraphQL :lock:
MIT License
472 stars 36 forks source link

Testing get an error with Rspec #13

Closed ducnguyenhuy-agilityio closed 6 years ago

ducnguyenhuy-agilityio commented 6 years ago

I have created a spec file as below:

require 'graphql/guard/testing'

RSpec.describe "GraphQL Guard" do
  it 'Authorization for rentals API' do
    rentals = Types::QueryType.field_with_guard('rentals', GraphqlPolicy)
    result = rentals.guard(obj, args, ctx)
    expect(result).to eq(true)
  end
end

When I try to run rspec, I got an error as followings:


NameError:
undefined local variable or method `obj' for #<RSpec::ExampleGroups::GraphQLGuard:0x0000559aa47de350>

Please let me know what I'm wrong?

exAspArk commented 6 years ago

Hey @ducnguyenhuy,

Probably the example in the README is a bit confusing. You should create and pass obj, args, ctx variables explicitly. For example:

rentals = Types::QueryType.field_with_guard('rentals', GraphqlPolicy)
object = Something.find(1)
arguments = {}
context = {current_user: User.find(1)} # or {}, for example

result = rentals.guard(object, arguments, context)

expect(result).to eq(true)

So, consider it as a unit-test where you explicitly pass input variables and get the output.

ducnguyenhuy-agilityio commented 6 years ago

@exAspArk It worked. Thanks. It will be better if you replace the above example with the example in README. Let me close this.