RSpec 调用中未定义方法“each”

RSpec undefined method `each' in call

在我的 Grape API 中,我有一个端点负责从 CMS webhook 接收数据 - 它运行良好但低于规格失败:

describe ::Webhooks::Cms::ReceiveWebhook, type: :request do
  subject(:call) { post endpoint, params: params, headers: basic_authorized_headers(auth_params) }

  let(:endpoint) { 'webhooks/Cms' }
  let(:params) { { some: 'params' } }
  let(:auth_params) do
    {
      username: Rails.application.credentials.cms_user,
      password: Rails.application.credentials.cms_password,
    }
  end

  it 'returns a successful response' do
    call
    expect(response).to be_successful
  end
end

helper with basic_authorized_headers 方法来自 headers:

module AuthRequestHelpers
  def basic_authorized_headers(username: nil, password: nil)
    "#{username}:#{password}"
  end
end

我遇到错误:

 Failure/Error: subject(:call) { post endpoint, params: params, headers: basic_authorized_headers(auth_params) }

 NoMethodError:
   undefined method `each' for "test@test.com:password":String

这是我的控制器:

module Cms
  class ReceiveWebhook < Base
    desc 'Receive data of CRUD actions from CMS webhook'

    http_basic do |user, password|
      user == Rails.application.credentials.cms_user &&
        password == Rails.application.credentials.cms_password
    end

    post :cms do
      status 200
    end
  end
end

post 需要 headers 参数的散列,您传递的是字符串。

subject(:call) { post endpoint, params: params, headers: { 'Authorization' => basic_authorized_headers(auth_params) } }

此外,通常 基本身份验证需要“基本”关键字,并且 credentials be encoded in Base64:

module AuthRequestHelpers
  def basic_authorized_headers(username: nil, password: nil)
    encoded_credentials = Base64.encode64("#{username}:#{password}")
    "Basic #{encoded_credentials}"
  end
end