[Rspec-devel] define_instance_method, stub_with, and mock_with

aslak hellesoy aslak.hellesoy at gmail.com
Sun Aug 27 18:33:32 EDT 2006


On 8/27/06, Brian Takita <brian.takita at gmail.com> wrote:
> Hello,
>
> At work we came up with a trio of methods that mock out methods in an
> object.
>
> define_instance_method
> stub_with
> mock_withFunny 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

Why not just do:

o = Object.new
def o.foo
  :bar
end
o.foo # return :bar

Is it just to get the raise when undefined methods are being (re)defined?

Aslak

>
> 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
>
>
> _______________________________________________
> Rspec-devel mailing list
> Rspec-devel at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-devel
>
>


More information about the Rspec-devel mailing list