Sha256: 2aa0dcdabdbc12fd2e3f1761bdb3e872853c782d2fae9c6f1b9d89e27449b878

Contents?: true

Size: 1001 Bytes

Versions: 4

Compression:

Stored size: 1001 Bytes

Contents

# RSpec matcher to spec alias.
RSpec::Matchers.define :alias_from do |method_name|
  match do |object|
    @method_name = method_name
    @object = object
    if !@object.respond_to?(@method_name)
      raise "#{@object} does not respond to ##{@method_name}!"
    end

    # If :alias_method was used
    returning_value = (@object.method(@to) == @object.method(@method_name))

    # Try something comparing return values
    if !returning_value
      @from_returned_value = double('receiver')
      @object.stub(@method_name).and_return(@from_returned_value)
      returning_value = (@object.send(@to) == @from_returned_value)
    end

    returning_value
  end

  description do
    "alias ##{@method_name} to ##{@to}"
  end

  failure_message_for_should do |text|
    "expected #{@object} to alias ##{@method_name} to ##{@to}"
  end

  failure_message_for_should_not do |text|
    "expected #{@object} NOT to alias ##{@method_name} to ##{@to}"
  end

  chain(:to) { |receiver| @to = receiver }
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ndlib-on-rspec-0.0.4 lib/ndlib-on-rspec/alias_from_matcher.rb
ndlib-on-rspec-0.0.3 lib/ndlib-on-rspec/alias_from_matcher.rb
ndlib-on-rspec-0.0.2 lib/ndlib-on-rspec/alias_from_matcher.rb
ndlib-on-rspec-0.0.1 lib/ndlib-on-rspec/alias_from_matcher.rb