Class/Module Index [+]

Quicksearch

Shoulda::Matchers::Independent

Matchers for non-Rails-dependent code.

Public Instance Methods

delegate_method(delegating_method) click to toggle source

The `delegate_method` matcher tests that an object forwards messages to other, internal objects by way of delegation.

In this example, we test that Courier forwards a call to deliver onto its PostOffice instance:

require 'forwardable'

class Courier
  extend Forwardable

  attr_reader :post_office

  def_delegators :post_office, :deliver

  def initialize
    @post_office = PostOffice.new
  end
end

# RSpec
describe Courier do
  it { should delegate_method(:deliver).to(:post_office) }
end

# Test::Unit
class CourierTest < Test::Unit::TestCase
  should delegate_method(:deliver).to(:post_office)
end

To employ some terminology, we would say that Courier’s deliver method is the delegating method, PostOffice is the delegate object, and PostOffice#deliver is the delegate method.

#### Qualifiers

##### as

Use `as` if the name of the delegate method is different from the name of the delegating method.

Here, Courier has a deliver method, but instead of calling deliver on the PostOffice, it calls ship:

class Courier
  attr_reader :post_office

  def initialize
    @post_office = PostOffice.new
  end

  def deliver(package)
    post_office.ship(package)
  end
end

# RSpec
describe Courier do
  it { should delegate_method(:deliver).to(:post_office).as(:ship) }
end

# Test::Unit
class CourierTest < Test::Unit::TestCase
  should delegate_method(:deliver).to(:post_office).as(:ship)
end

##### with_arguments

Use `with_arguments` to assert that the delegate method is called with certain arguments.

Here, when Courier#deliver calls PostOffice#ship, it adds an options hash:

class Courier
  attr_reader :post_office

  def initialize
    @post_office = PostOffice.new
  end

  def deliver(package)
    post_office.ship(package, expedited: true)
  end
end

# RSpec
describe Courier do
  it do
    should delegate_method(:deliver).
      to(:post_office).
      as(:ship).
      with_arguments(expedited: true)
  end
end

# Test::Unit
class CourierTest < Test::Unit::TestCase
  should delegate_method(:deliver).
    to(:post_office).
    as(:ship).
    with_arguments(expedited: true)
end

@return [DelegateMatcher]

# File lib/shoulda/matchers/independent/delegate_matcher.rb, line 110
def delegate_method(delegating_method)
  DelegateMatcher.new(delegating_method)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.