Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

String keys should be allowed in **kwargs #554

Closed
@johnnymo87

Description

@johnnymo87

Subject of the issue

Ruby 2.7 introduce the feature of last-argument non-specified keyword argument **kwargs in method signatures. In this particular kind of keyword argument, String keys are allowed. But in this library's MethodSignature logic, we're counting on keyword arguments of any kind always being Symbol keys (1, 2). So in the case of **kwargs with String keys, we misinterpret them and treat them as intended for a positional argument.

This issue is similar to rspec/rspec#104, but I believe it's not addressed by the WIP #537, so I figured I'd open a separate issue to highlight it.

Your environment

  • Ruby version: 3.1
  • rspec-support version: 3.11

Steps to reproduce

Run the following file:

require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'
  gem 'rspec'
end

require 'rspec/autorun'

RSpec.configure do |config|
  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end
end

module MyModule
  def self.my_func(arg1, **kwargs); end
end

RSpec.describe MyModule do
  it 'works with kwargs with String keys' do
    expect(MyModule).to receive(:my_func).with(1, 'foo' => 'bar')

    MyModule.my_func(1, 'foo' => 'bar')
  end
end

Expected behavior

The spec should pass.

Actual behavior

Fails with the following error:

     Failure/Error: expect(MyModule).to receive(:my_func).with(1, 'foo' => 'bar')
       Wrong number of arguments. Expected 1, got 2.

Note that this passes if I change 'foo' => 'bar' to :foo => 'bar'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions