You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

测试环境无法绕过Omniauth初始化,GraphQL Schema测试受阻

问题:测试GraphQL Schema时触发Omniauth认证报错

我最近在尝试无需认证的情况下测试GraphQL Schema,已经在GraphqlController里添加了:

skip_before_action :verify_authenticity_token

在开发环境中,用Postman复制GraphiQL的curl请求可以成功执行查询:

  • 请求体:
    {"query":"{\n user(id: 1) {\n id\n created_at\n updated_at\n jwt\n}\n}\n","variables":null,"operationName":null}
    
  • 请求头设置Content-Type: application/json,整个请求完全正常。

但到了测试环境,请求触发了Omniauth的初始化代码:

Rails.application.config.middleware.use OmniAuth::Builder do 
  provider( 
    :auth0, 
    Auth0::Config["app_client_id"], 
    Auth0::Config["app_client_secret"], 
    Auth0::Config["domain"], 
    callback_path: "/auth/auth0/callback" 
  ) 
end

我希望测试这个请求的时候完全不需要任何认证头,但现在运行RSpec测试时出了问题。

我的RSpec共享上下文代码如下:

require 'graphlient'
RSpec.shared_context "GraphQL Client", shared_context: :metadata do
  let(:client) do
    Graphlient::Client.new('https://api.example.org/graphql') do |client|
      client.http do |h|
        h.connection do |c|
          c.use Faraday::Adapter::Rack, app
        end
      end
    end
  end
end

实际测试代码:

it 'retrieves schema' do
  expect(client.schema).to be_a GraphQL::Schema
end

运行测试后报错:

Failure/Error: expect { client.schema.status } .to raise_error(Graphlient::Errors::ServerError)
expected Graphlient::Errors::ServerError, got #<ArgumentError: Received wrong number of arguments. [nil, nil, nil, {:callback_path=>"/auth/auth0/callback"}]> with backtrace:
# /usr/local/bundle/gems/omniauth-auth0-1.4.2/lib/omniauth/strategies/auth0.rb:41:in `initialize'

内容的提问来源于stack exchange,提问作者madav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:36:10