diff --git a/CHANGELOG.md b/CHANGELOG.md index 9deb379..e7dda39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Added/Changed/Deprecated/Removed/Fixed/Security: YOUR CHANGE HERE * 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 ## [3.1.0](2025-05-26) diff --git a/docs/testing/testing.md b/docs/testing/testing.md index 49a5f4a..c538a38 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` +* `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` ### Test examples diff --git a/lib/graphql_rails/rspec_controller_helpers.rb b/lib/graphql_rails/rspec_controller_helpers.rb index d519b8f..2e77fb1 100644 --- a/lib/graphql_rails/rspec_controller_helpers.rb +++ b/lib/graphql_rails/rspec_controller_helpers.rb @@ -54,6 +54,10 @@ def action_name request.action_name end + def context + request.context + end + private attr_reader :request diff --git a/spec/lib/graphql_rails/rspec_controller_helpers_spec.rb b/spec/lib/graphql_rails/rspec_controller_helpers_spec.rb index bd1583f..cbf9076 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 '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 + end + context 'when request was successful' do it 'sets status to success' do runner.query(:index, params: action_params, context: action_context)