class Mocha::ClassMethod
Attributes
method[R]
stubbee[R]
Public Class Methods
new(stubbee, method)
click to toggle source
# File lib/mocha/class_method.rb, line 9 def initialize(stubbee, method) @stubbee = stubbee @original_method, @original_visibility = nil, nil @method = RUBY_VERSION < '1.9' ? method.to_s : method.to_sym end
Public Instance Methods
define_new_method()
click to toggle source
# File lib/mocha/class_method.rb, line 56 def define_new_method stubbee.__metaclass__.class_eval(%Q{ def #{method}(*args, &block) mocha.method_missing(:#{method}, *args, &block) end }, __FILE__, __LINE__) end
hide_original_method()
click to toggle source
# File lib/mocha/class_method.rb, line 37 def hide_original_method if method_exists?(method) begin @original_method = stubbee._method(method) @original_visibility = :public if stubbee.__metaclass__.protected_instance_methods.include?(method) @original_visibility = :protected elsif stubbee.__metaclass__.private_instance_methods.include?(method) @original_visibility = :private end if @original_method && @original_method.owner == stubbee.__metaclass__ stubbee.__metaclass__.send(:remove_method, method) end rescue NameError # deal with nasties like ActiveRecord::Associations::AssociationProxy end end end
matches?(other)
click to toggle source
# File lib/mocha/class_method.rb, line 84 def matches?(other) return false unless (other.class == self.class) (stubbee.object_id == other.stubbee.object_id) and (method == other.method) end
method_exists?(method)
click to toggle source
# File lib/mocha/class_method.rb, line 95 def method_exists?(method) symbol = method.to_sym __metaclass__ = stubbee.__metaclass__ __metaclass__.public_method_defined?(symbol) || __metaclass__.protected_method_defined?(symbol) || __metaclass__.private_method_defined?(symbol) end
mock()
click to toggle source
# File lib/mocha/class_method.rb, line 29 def mock stubbee.mocha end
remove_new_method()
click to toggle source
# File lib/mocha/class_method.rb, line 64 def remove_new_method stubbee.__metaclass__.send(:remove_method, method) end
reset_mocha()
click to toggle source
# File lib/mocha/class_method.rb, line 33 def reset_mocha stubbee.reset_mocha end
restore_original_method()
click to toggle source
# File lib/mocha/class_method.rb, line 68 def restore_original_method if @original_method && @original_method.owner == stubbee.__metaclass__ if RUBY_VERSION < '1.9' original_method = @original_method stubbee.__metaclass__.send(:define_method, method) do |*args, &block| original_method.call(*args, &block) end else stubbee.__metaclass__.send(:define_method, method, @original_method) end end if @original_visibility Module.instance_method(@original_visibility).bind(stubbee.__metaclass__).call(method) end end
stub()
click to toggle source
# File lib/mocha/class_method.rb, line 15 def stub hide_original_method define_new_method end
to_s()
click to toggle source
# File lib/mocha/class_method.rb, line 91 def to_s "#{stubbee}.#{method}" end
unstub()
click to toggle source
# File lib/mocha/class_method.rb, line 20 def unstub remove_new_method restore_original_method mock.unstub(method.to_sym) unless mock.any_expectations? reset_mocha end end