public
Description: Stubbing and mocking that doesn't hurt or suck. WIN.
Homepage:
Clone URL: git://github.com/jeremymcanally/stump.git
Click here to lend your support to: stump and make a donation at www.pledgie.com !
stump / lib / stump / stub.rb
100644 34 lines (31 sloc) 0.827 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Object
  # Create a stub method on an object. Simply returns a value for a method call on
  # an object.
  #
  # ==== Examples
  #
  # my_string = "a wooden rabbit"
  # my_string.stub!(:retreat!, :return => "run away! run away!")
  #
  # # test/your_test.rb
  # my_string.retreat! # => "run away! run away!"
  #
  def stub!(method, options = {}, &block)
    behavior = (block_given? ? block : proc { return options[:return] })
 
    meta_def method, &behavior
  end
end
 
module Kernel
  # Create a pure stub object.
  #
  # ==== Examples
  #
  # stubbalicious = stub(:failure, "wat u say?")
  # stubbalicious.failure # => "wat u say?"
  #
  def stub(method, options = {}, &block)
    stub_object = Object.new
    stub_object.stub!(method, options, &block)
    
    stub_object
  end
end