Skip to content

Instantly share code, notes, and snippets.

@bradphelan
Created March 19, 2010 17:01
Show Gist options
  • Save bradphelan/337835 to your computer and use it in GitHub Desktop.
Save bradphelan/337835 to your computer and use it in GitHub Desktop.
class Object
def should_receive!(sym, opts={}, &block)
objx = Object.new
@__rspec_obj__ ||= Object.new
m = class<<self;self;end
sym_old = "__#{sym}__old__"
# Generates for sym => :foo
#
# alias :foo_old :foo
#
# def foo *args, &block
# foo_old *args, &block
# @__rspec_obj__.send :foo, *args, &block
# end
if not respond_to?("#{sym_old}".to_sym)
m.class_eval <<-EOS, __FILE__, __LINE__
alias :#{sym_old} :#{sym}
def #{sym} *args, &block
#{sym_old} *args, &block
@__rspec_obj__.send :#{sym}, *args, &block
end
EOS
end
@__rspec_obj__.should_receive(sym, opts, &block)
end
end
class Fing
def bar
puts " bar really called"
end
def bing
puts " bing really called"
end
end
describe Fing do
it "is a monkey" do
b = Fing.new
b.should_receive!(:bar).once.ordered
b.should_receive!(:bing).once.ordered
b.should_receive!(:bar).once.ordered
b.bar
b.bing
b.bar
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment