Class: RSpec::Mocks::ClassNewMethodReference

Inherits:
ObjectMethodReference show all
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/method_reference.rb

Overview

When a class’s ‘.new` method is stubbed, we want to use the method signature from `#initialize` because `.new`’s signature is a generic ‘def new(*args)` and it simply delegates to `#initialize` and forwards all args…so the method with the actually used signature is `#initialize`.

This method reference implementation handles that specific case.

Constant Summary collapse

CLASS_NEW =
::Class.instance_method(:new)

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ObjectMethodReference

for

Methods inherited from MethodReference

#defined?, for, #implemented?, #initialize, instance_method_visibility_for, method_visibility_for, #unimplemented?, #visibility

Constructor Details

This class inherits a constructor from RSpec::Mocks::MethodReference

Class Method Details

.applies_to?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


185
186
187
188
189
190
191
192
193
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/method_reference.rb', line 185

def self.applies_to?(method_name)
  return false unless method_name == :new
  klass = yield
  return false unless ::Class === klass && klass.respond_to?(:new, true)

  # We only want to apply our special logic to normal `new` methods.
  # Methods that the user has monkeyed with should be left as-is.
  uses_class_new?(klass)
end

.uses_class_new?(klass) ⇒ Boolean

Ruby 2’s Method#== is too strict

Returns:

  • (Boolean)


202
203
204
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/method_reference.rb', line 202

def self.uses_class_new?(klass)
  ::RSpec::Support.method_handle_for(klass, :new) == CLASS_NEW.bind(klass)
end

Instance Method Details

#with_signatureObject



207
208
209
210
211
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/method_reference.rb', line 207

def with_signature
  @object_reference.when_loaded do |klass|
    yield Support::MethodSignature.new(klass.instance_method(:initialize))
  end
end