rspec / rspec-mocks

RSpec's 'test double' framework, with support for stubbing and mocking
https://rspec.info
MIT License
1.16k stars 357 forks source link

RSpec 3.11 mistakes kwargs for hash, when `verify_partial_doubles` is set to true #1549

Closed 64kramsystem closed 1 year ago

64kramsystem commented 1 year ago

Subject of the issue

When verify_partial_doubles is set to true, RSpec 3.11 reports some failures (on Ruby 3.2), mistaking kwargs for hashes; this does not happen on RSpec 3.12.

The kwargs/hashes problem is a known dev mistake (on Ruby 3.2), however, it seems not to be the cause here, because if this was the case, RSpec would report a failure on both 3.11 and 3.12; regardless, it should be unrelated to verify_partial_doubles.

The failure output is in this form:

        expected: ([foo, bar], {:baz=>qux})
            got: ([foo, bar], {:baz=>qux})

Your environment

Steps to reproduce

There you go:

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  gem "rspec", "3.11.0"
end

puts "Ruby version is: #{RUBY_VERSION}"
require 'rspec/autorun'

RSpec.configure do |config|
  config.mock_with :rspec do |mocks|
    #######################################################################
    # PROBLEM IS HERE; EXPECTATION FAILS WHEN THIS IS SET TO TRUE
    #######################################################################
    mocks.verify_partial_doubles = true
  end
end

class MyClass
  def start_service(myparam: nil)
  end

  def restart_service(myparam: nil)
    start_service(myparam: 'foo')
  end
end

describe MyClass do
  it 'should pass' do
    expect(subject).to receive(:start_service).with(myparam: 'foo')
    subject.restart_service(myparam: 'foo')
  end
end

Expected behavior

The expectation should be met.

Actual behavior

The expectation is not met, because the kwargs are mistaken for a hash.

pirj commented 1 year ago

This is fixed by #1514, and released in 3.12.2. Please use a more recent version. I believe we don’t backport fixes to prior minor releases.

JonRowe commented 1 year ago

Apologies but as @pirj said we don't backport fixes to older minor versions, only maintaining the current major/minor release stream, you should be able to upgrade to 3.12 seamlessly as there are no breaking changes.

64kramsystem commented 1 year ago

Thanks for looking :pray:.

(this is a PITA for us, because One Famous Gem™ (chef cough cough) transitively depends on rspec (!!!), worse, restricting the version to <= 3.11)

pirj commented 1 year ago

https://github.com/inspec/inspec/pull/6523