diff --git a/CHANGELOG.md b/CHANGELOG.md index e7dda39..0ffdfed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Changed: do not throw "Cannot return null for non-nullable" error when errors are manually rendered * Added: allow to specify type error hooks in router * Added: `response.context` in RSpec controller helpers +* Added: `dig` on the RSpec controller helpers test context, as on `GraphQL::Query::Context` ## [3.1.0](2025-05-26) diff --git a/docs/testing/testing.md b/docs/testing/testing.md index c538a38..42539fc 100644 --- a/docs/testing/testing.md +++ b/docs/testing/testing.md @@ -22,7 +22,7 @@ There are 3 helper methods: * `mutation(:your_controller_action_name, params: {}, context: {})`. `params` and `context` are optional * `query(:your_controller_action_name, params: {}, context: {})`. `params` and `context` are optional -* `response`. Response is set only after you call `mutation` or `query`. It exposes, among others, `result`, `errors`, `success?`, `controller`, `action_name` and `context`: the hash-like test context the action ran with, not a `GraphQL::Query::Context` +* `response`. Response is set only after you call `mutation` or `query`. It exposes, among others, `result`, `errors`, `success?`, `controller`, `action_name` and `context`: the hash-like test context the action ran with (it supports `[]`, `fetch`, `key?`, `dig` and `to_h`), not a `GraphQL::Query::Context` ### Test examples diff --git a/lib/graphql_rails/rspec_controller_helpers.rb b/lib/graphql_rails/rspec_controller_helpers.rb index 2e77fb1..a5f3c3d 100644 --- a/lib/graphql_rails/rspec_controller_helpers.rb +++ b/lib/graphql_rails/rspec_controller_helpers.rb @@ -69,7 +69,7 @@ class FakeContext attr_reader :schema - def_delegators :@provided_values, :[], :[]=, :to_h, :key?, :fetch + def_delegators :@provided_values, :[], :[]=, :to_h, :key?, :fetch, :dig def initialize(values:, schema:) @errors = [] diff --git a/spec/lib/graphql_rails/rspec_controller_helpers_spec.rb b/spec/lib/graphql_rails/rspec_controller_helpers_spec.rb index cbf9076..91f21d5 100644 --- a/spec/lib/graphql_rails/rspec_controller_helpers_spec.rb +++ b/spec/lib/graphql_rails/rspec_controller_helpers_spec.rb @@ -96,6 +96,11 @@ def boom expect(runner.response.action_name).to eq :index end + it 'lets the context be read with dig' do + runner.query(:index, params: action_params, context: { api: { version: 2 } }) + expect(runner.response.context.dig(:api, :version)).to eq 2 + end + it 'includes the context the action ran with' do runner.query(:index, params: action_params, context: action_context) expect(runner.response.context.to_h).to eq action_context