[Rspec-devel] define_instance_method, stub_with, and mock_with

Brian Takita brian.takita at gmail.com
Sun Aug 27 14:47:23 EDT 2006


Hello,

At work we came up with a trio of methods that mock out methods in an
object.

   - define_instance_method
   - stub_with
   - mock_with

Funny thing is around the same time, this was posted.
http://blog.seagul.co.uk/articles/2006/06/30/very-very-lightweight-mocking-ish

Here is a snippit from that article.

class Object

  def metaclass
    class << self; self; end
  end

  def define_instance_method(sym, &block)
    metaclass.__send__(:define_method, sym, &block)
  end

  def stub_instance_method(sym, &block)
    raise "#{self} does not respond to <#{sym}> and therefore cannot be
stubbed" unless self.respond_to?(sym)
    define_instance_method(sym, &block)
  end

  def __log__
    @__log__ ||= []
  end

end

So you can use this like:
o = Object.new
o.define_instance_method(:foo) do
  :bar
end
o.foo # return :bar

We went a little further by adding the mock_with and stub_with methods,
which take a Hash and allow you to mock out the method names corresponding
to the keys.

For example,
o = Object.new
two_value = 2
o.stub_with(:one => 1, :two => proc {two_value})
o.one # returns 1
o.two # returns 2

o = Object.new
mock_action = mock("mock_action")
mock_action.should_receive(:something).with(:grey_poupon)
o.mock_with(:receive => proc {mock_action)}
o.receive.something(:grey_poupon)

David, I also remember you where working on acts_as_mock. This has helped us
with our tests and I think it would also make using specs more convenient
and easier to read.
What does everybody think? If it sounds good, I'll create a patch.

Thanks,
Brian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-devel/attachments/20060827/3452cd3d/attachment.html 


More information about the Rspec-devel mailing list