rspec-mocks: class spies fail to verify when chaining an instance method onto a class method that returns an instance

Ran into this issue today, which I’ve reproduced approximately here:

class Foo
  def self.find
    new
  end

  def bar
    'bar'
  end
end

class Baz
  def qux
    Foo.find.bar
    'qux'
  end
end

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

RSpec.describe 'verifying doubles' do
  it 'raises an error when a chaining an instance method onto a class method that returns an instance' do
    foo_class = class_spy(Foo).as_stubbed_const
    expect(Baz.new.qux).to eq('qux')
  end
end

Basically I want to ignore some implementation detail of Baz by stubbing out Foo. I get the following error however on executing this test:

  1) verifying doubles raises an error when a chaining an instance method onto a class method that returns an instance
     Failure/Error: Foo.find.bar
       Foo does not implement: bar
     # ./verifying_doubles.rb:13:in `qux'
     # ./verifying_doubles.rb:27:in `block (2 levels) in <top (required)>'

Could this be an issue? Or could it be considered bad practice to use a class spy like this? Thanks!

About this issue

Most upvoted comments