Class: Sinclair::Matchers::AddMethodTo Private

Inherits:
RSpec::Matchers::BuiltIn::BaseMatcher
  • Object
show all
Defined in:
lib/sinclair/matchers/add_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.

AddMethodTo 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

Instance Method Summary collapse

Constructor Details

#initialize(klass, method) ⇒ AddMethodTo #initialize(instance, method) ⇒ AddMethodTo

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 AddMethodTo

Overloads:

  • #initialize(klass, method) ⇒ AddMethodTo

    Parameters:

    • klass (Class)

      class where the method should be added to

  • #initialize(instance, method) ⇒ AddMethodTo

    Parameters:

    • klass (Object)

      instance of the class where the method should be added to

Parameters:

  • method

    method name



49
50
51
52
53
54
55
56
# File 'lib/sinclair/matchers/add_method_to.rb', line 49

def initialize(target, method)
  if target.is_a?(Class)
    @klass = target
  else
    @instance = target
  end
  @method = method
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)


63
64
65
# File 'lib/sinclair/matchers/add_method_to.rb', line 63

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

#equal?(other) ⇒ Boolean Also known as: ==

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.

Checkes if another instnce is equal self

Returns:

  • (Boolean)


108
109
110
111
112
# File 'lib/sinclair/matchers/add_method_to.rb', line 108

def equal?(other)
  return unless other.class == self.class
  other.method == method &&
    other.instance == instance
end

#failure_message_for_shouldString Also known as: failure_message

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)


72
73
74
75
# File 'lib/sinclair/matchers/add_method_to.rb', line 72

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

#failure_message_for_should_notString Also known as: failure_message_when_negated

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)


82
83
84
# File 'lib/sinclair/matchers/add_method_to.rb', line 82

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

#matches?(event_proc) ⇒ 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.

Checks if expectation is true or not

Returns:

  • (Boolean)

    expectation check



91
92
93
94
95
96
# File 'lib/sinclair/matchers/add_method_to.rb', line 91

def matches?(event_proc)
  return false unless event_proc.is_a?(Proc)
  raise_block_syntax_error if block_given?
  perform_change(event_proc)
  added?
end

#supports_block_expectations?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.

definition needed for block matchers

Returns:

  • (Boolean)


99
100
101
# File 'lib/sinclair/matchers/add_method_to.rb', line 99

def supports_block_expectations?
  true
end