Method: Object#proxy!

Defined in:
lib/stump/proxy.rb

#proxy!(method, options = {}, &block) ⇒ Object

Creates a proxy method on an object. In this setup, it places an expectation on an object (like a mock) but still calls the original method. So if you want to make sure the method is called and still return its value, or simply want to invoke the side effects of a method and return a stubbed value, then you can do that.

Examples

class Parrot
  def speak!
    puts @words
  end

  def say_this(words)
    @words = words
    "I shall say #{words}!"
  end
end

# => test/your_test.rb
sqawky = Parrot.new
sqawky.proxy!(:say_this)
# Proxy method still calls original method...
sqawky.say_this("hey")   # => "I shall say hey!"
sqawky.speak!            # => "hey"

sqawky.proxy!(:say_this, "herro!")
# Even though we return a stubbed value...
sqawky.say_this("these words")   # => "herro!"
# ...the side effects are still there.
sqawky.speak!                    # => "these words"

TODO: This implementation is still very rough. Needs refactoring and refining. Won’t work on ActiveRecord attributes, for example.



36
37
38
39
40
41
42
43
44
# File 'lib/stump/proxy.rb', line 36

def proxy!(method, options = {}, &block)
  Stump::Mocks.add([self, method])
  
  if respond_to?(method)
    proxy_existing_method(method, options, &block)
  else
    proxy_missing_method(method, options, &block)
  end
end