Class: Sinclair::Matchers::AddInstanceMethodTo Private

Inherits:
AddMethodTo show all
Defined in:
lib/sinclair/matchers/add_instance_method_to.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.

AddInstanceMethodTo checks whether a method was or not added by the call of a block

This is used with a RSpec DSL method add_method(method_name).to(class_object)

Examples:

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

class MyModel
end

RSpec.describe 'my test' do
  let(:klass)   { Class.new(MyModel) }
  let(:builder) { Sinclair.new(klass) }

  before do
    builder.add_method(:class_name, 'self.class.name')
  end

  it do
    expect { builder.build }.to add_method(:class_name).to(klass)
  end
end

Author:

  • darthjee

Instance Method Summary collapse

Methods included from MethodTo

#failure_message, #failure_message_when_negated, #matches?

Methods inherited from Base

#equal?, #supports_block_expectations?

Constructor Details

#initialize(klass, method_name) ⇒ AddInstanceMethodTo #initialize(instance, method_name) ⇒ 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.

Returns a new instance of AddInstanceMethodTo.

Overloads:

  • #initialize(klass, method_name) ⇒ AddInstanceMethodTo

    Parameters:

    • klass (Class)

      class where the method should be added to

  • #initialize(instance, method_name) ⇒ AddInstanceMethodTo

    Parameters:

    • instance (Object)

      instance of the class where the method should be added to

Parameters:

  • method_name (Symbol, String)

    method name



42
43
44
45
46
47
48
49
# File 'lib/sinclair/matchers/add_instance_method_to.rb', line 42

def initialize(target, method_name)
  if target.is_a?(Class)
    @klass = target
  else
    @instance = target
  end
  super(method_name)
end

Instance Method Details

#descriptionString

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.

Returnst expectaton description

Returns:

  • (String)


54
55
56
# File 'lib/sinclair/matchers/add_instance_method_to.rb', line 54

def description
  "add method '#{method_name}' to #{klass} instances"
end

#failure_message_for_shouldString

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.

Returns message on expectation failure

Returns:

  • (String)


61
62
63
64
# File 'lib/sinclair/matchers/add_instance_method_to.rb', line 61

def failure_message_for_should
  "expected '#{method_name}' to be added to #{klass} but " \
    "#{initial_state ? 'it already existed' : "it didn't"}"
end

#failure_message_for_should_notString

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.

Returns message on expectation failure for negative expectation

Returns:

  • (String)


69
70
71
# File 'lib/sinclair/matchers/add_instance_method_to.rb', line 69

def failure_message_for_should_not
  "expected '#{method_name}' not to be added to #{klass} but it was"
end