class Object
Public Instance Methods
expect_call( method_name, params, val_or_callable = nil ) { || ... }
click to toggle source
# File lib/expect-call.rb, line 2 def expect_call( method_name, params, val_or_callable = nil, &block ) context = block.binding.eval( 'self' ) if context.respond_to?( :assert ) assert_method = context.method( :assert ) else assert_method = Proc.new do |condition, message| raise RuntimeError, "expect-call assertion\n#{ message }" unless condition end end metaclass = class << self; self; end orig_method = nil if respond_to? method_name orig_method = method( method_name ) metaclass.send :undef_method, method_name end clean_up = Proc.new do metaclass.send :undef_method, method_name if orig_method metaclass.send :define_method, method_name, orig_method end end was_called = false new_method = Proc.new do |*args| was_called = true clean_up.call assert_method.call params == args, <<-MSG % [ method_name, params.inspect, args.inspect ] Method '%s' called with unexpected arguments Expected: %s Actual: %s MSG if val_or_callable.respond_to?( :call ) return val_or_callable.call( *args ) end val_or_callable end metaclass.send :define_method, method_name, new_method yield assert_method.call was_called, "Method '%s' was not called" % [ method_name ] ensure clean_up.call unless was_called end