Sha256: 6a89e283edd1a555851dcdcf1122d25d7e50acef05140172ccbc1d83d7bdb446
Contents?: true
Size: 1.4 KB
Versions: 14
Compression:
Stored size: 1.4 KB
Contents
require 'mocha/class_method' module Mocha class AnyInstanceMethod < ClassMethod def mock stubbee.any_instance.mocha end def reset_mocha stubbee.any_instance.reset_mocha end def hide_original_method if method_exists?(method) begin stubbee.send(:alias_method, hidden_method, method) rescue NameError # deal with nasties like ActiveRecord::Associations::AssociationProxy end end end def define_new_method stubbee.class_eval(%{ def #{method}(*args, &block) self.class.any_instance.mocha.method_missing(:#{method}, *args, &block) end }, __FILE__, __LINE__) end def remove_new_method stubbee.send(:remove_method, method) end def restore_original_method if method_exists?(hidden_method) begin stubbee.send(:alias_method, method, hidden_method) stubbee.send(:remove_method, hidden_method) rescue NameError # deal with nasties like ActiveRecord::Associations::AssociationProxy end end end def method_exists?(method) return true if stubbee.public_instance_methods(false).include?(method) return true if stubbee.protected_instance_methods(false).include?(method) return true if stubbee.private_instance_methods(false).include?(method) return false end end end
Version data entries
14 entries across 14 versions & 3 rubygems