Class: Sinclair::Matchers::AddInstanceMethod Private

Inherits:
AddMethod
  • Object
show all
Defined in:
lib/sinclair/matchers/add_instance_method.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

AddInstanceMethod is able to build an instance of AddInstanceMethodTo

Examples:

Using inside RSpec and checking Class

RSpec.configure do |config|
  config.include Sinclair::Matchers
end

class MyModel
end

RSpec.describe "MyBuilder" do
  let(:clazz)   { Class.new(MyModel) }
  let(:builder) { Sinclair.new(clazz) }

  before do
    builder.add_method(:new_method, "2")
  end

  it do
    expect { builder.build }.to add_method(:new_method).to(clazz)
  end
end

# Outputs
# 'should add method 'new_method' to #<Class:0x000056441bf46608> instances'

Using inside RSpec and checking instance

RSpec.configure do |config|
  config.include Sinclair::Matchers
end

class MyModel
end

RSpec.describe "MyBuilder" do
  let(:clazz)    { Class.new(MyModel) }
  let(:builder)  { Sinclair.new(clazz) }
  let(:instance) { clazz.new }

  before do
    builder.add_method(:the_method, "true")
  end

  it do
    expect { builder.build }.to add_method(:the_method).to(instance)
  end
end

# Outputs
# 'should add method 'the_method' to #<Class:0x000056441bf46608> instances'

Author:

  • darthjee

Instance Method Summary collapse

Methods inherited from AddMethod

#equal?, #initialize, #supports_block_expectations?

Constructor Details

This class inherits a constructor from Sinclair::Matchers::AddMethod

Instance Method Details

#matches?(_actual) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method is abstract.

Raise a warning on the usage as this is only a builder for Sinclair::Matchers::AddInstanceMethodTo

Returns:

  • (Boolean)

Raises:

  • SyntaxError



64
65
66
67
# File 'lib/sinclair/matchers/add_instance_method.rb', line 64

def matches?(_actual)
  raise SyntaxError, 'You should specify which instance the method is being added to' \
    "add_method(:#{method}).to(instance)"
end

#to(klass) ⇒ AddInstanceMethodTo #to(instance) ⇒ AddInstanceMethodTo

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a matcher AddInstanceMethodTo

Overloads:

  • #to(klass) ⇒ AddInstanceMethodTo

    Parameters:

    • klass (Class)

      class where the method should be added to

  • #to(instance) ⇒ AddInstanceMethodTo

    Parameters:

    • instance (Object)

      instance of the class where the method should be added to

Returns:



80
81
82
# File 'lib/sinclair/matchers/add_instance_method.rb', line 80

def to(target = nil)
  AddInstanceMethodTo.new(target, method)
end