Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.and_call_original breaks on Ruby 3.2 for methods with keyword arguments #1512

Closed
ric2b opened this issue Dec 30, 2022 · 6 comments · Fixed by #1514
Closed

.and_call_original breaks on Ruby 3.2 for methods with keyword arguments #1512

ric2b opened this issue Dec 30, 2022 · 6 comments · Fixed by #1514

Comments

@ric2b
Copy link

ric2b commented Dec 30, 2022

Subject of the issue

With Ruby 3.2 rspec-mocks seems to not pass keyword arguments to the original implementation of a method when using .and_call_original

Your environment

  • Ruby version: 3.2.0p0
  • rspec-mocks version: 3.12.1

Steps to reproduce

I was able to create this minimal test that reproduces the issue:

      it "handles keyword arguments correctly" do
        A = Class.new do
          def initialize(kw:)
          end
        end

        A.new(kw: 42)

        allow(A).to receive(:new).and_call_original

        A.new(kw: 42)
      end

Expected behavior

The test should pass

Actual behavior

The test fails on the second A instantiation with the error:
ArgumentError: wrong number of arguments (given 1, expected 0; required keyword: kw)

@ric2b ric2b changed the title Ruby 3.2 breaks .and_call_original for methods with keyword arguments .and_call_original breaks on Ruby 3.2 for methods with keyword arguments Dec 30, 2022
@pirj
Copy link
Member

pirj commented Dec 30, 2022

Thanks for reporting. Does it help to insert

ruby2_keywords msg if respond_to?(:ruby2_keywords, true)

here?

@ric2b
Copy link
Author

ric2b commented Dec 30, 2022

Doesn't seem to make a difference :/

@pirj
Copy link
Member

pirj commented Jan 3, 2023

Can you please check if #1514 fixes this case?

@ric2b
Copy link
Author

ric2b commented Jan 3, 2023

@pirj Yup, that seems to fix it!

By the way, I'm not sure what the simplest way of running a specific branch of a dependency is, I struggled a bit, here's what I tried:

  1. I first tried bundle add rspec-mocks --github rspec/rspec-mocks --branch ruby-3.2 but got the error (to be clear, the version is not pinned in my Gemfile, this is about Gemfile.lock):

    Your bundle is locked to rspec-mocks (3.12.1) from https://github.com/rspec/rspec-mocks.git (at e9d65cd@e9d65cd), but that version can no longer be found in that source. That means the author of rspec-mocks
    (3.12.1) has removed it. You'll need to update your bundle to a version other than rspec-mocks (3.12.1) that hasn't been removed in order to install.

  2. So then I tried bundle config set local.rspec-mocks /tmp/rspec-mocks/ (after cloning and using git switch ruby-3.2) and that seemed to set the branch correctly but the test was still failing as bundle was not using my local clone to run the test.

  3. I ran bundle add rspec-mocks --github rspec/rspec-mocks --branch ruby-3.2 again following the docs but it had other failures due to the branch version being 3.13.0.pre. I manually edited it to 3.12.1 and bundle finally seemed to accept my local clone and use it when running the test.

@Mange
Copy link

Mange commented Jan 4, 2023

@ric2b Here's how I tested that branch:

  %w[rspec-core rspec-expectations rspec-support rspec-rails].each do |lib|
    gem lib, git: "https://github.com/rspec/#{lib}.git", branch: "main"
  end
  gem "rspec-mocks", git: "https://github.com/rspec/rspec-mocks.git", branch: "ruby-3.2"

It's based on the snippet in the README of this repo, except rspec-mocks use a different branch than the rest.

Note that I also include rspec-rails since my project was using that too.

@Mange
Copy link

Mange commented Jan 4, 2023

I had a related problem in that branch. Perhaps this isn't fixed, or perhaps the fix broke something else.

It's detailed in the PR of that branch:
#1514 (comment)

@pirj pirj closed this as completed in #1514 Jan 4, 2023
mvach added a commit to cloudfoundry/bosh-azure-cpi-release that referenced this issue Apr 10, 2024
After updating to Ruby 3.2.3 we have been hit by rspec/rspec-mocks#1512
of rspec.  Therefore we update to the latest rspec version here.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants