Sha256: 5fe65288b2e75590cbd9bb0a02fe318919cb15dd0d5f932bf74ba0d94c6072da

Contents?: true

Size: 1.45 KB

Versions: 21

Compression:

Stored size: 1.45 KB

Contents

require 'mocha/class_method'

module Mocha

  class AnyInstanceMethod < ClassMethod
  
    def unstub
      remove_new_method
      restore_original_method
      stubbee.any_instance.reset_mocha
    end
    
    def mock
      stubbee.any_instance.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

21 entries across 21 versions & 6 rubygems

Version Path
floehopper-mocha-0.9.6.20090629164857 lib/mocha/any_instance_method.rb
floehopper-mocha-0.9.6.20090629165308 lib/mocha/any_instance_method.rb
floehopper-mocha-0.9.6.20090701111305 lib/mocha/any_instance_method.rb
floehopper-mocha-0.9.7.20090701124354 lib/mocha/any_instance_method.rb
jferris-mocha-0.9.7.0.1247796736 lib/mocha/any_instance_method.rb
jferris-mocha-0.9.7.20090701124354 lib/mocha/any_instance_method.rb
jferris-mocha-0.9.7.20090911190113 lib/mocha/any_instance_method.rb
mocha-0.10.1 lib/mocha/any_instance_method.rb
vanity-1.7.1 vendor/ruby/1.9.1/gems/mocha-0.9.12/lib/mocha/any_instance_method.rb
mocha-0.10.0 lib/mocha/any_instance_method.rb
mocha-0.9.12 lib/mocha/any_instance_method.rb
mocha-0.9.11 lib/mocha/any_instance_method.rb
mocha-0.9.10 lib/mocha/any_instance_method.rb
mocha-macruby-0.9.9.20101102121900 lib/mocha/any_instance_method.rb
mocha-0.9.9 lib/mocha/any_instance_method.rb
jferris-mocha-0.9.8.20100526112143 lib/mocha/any_instance_method.rb
mocha-macruby-0.9.8.20100129120100 lib/mocha/any_instance_method.rb
mcmire-mocha-0.9.8 lib/mocha/any_instance_method.rb
mocha-0.9.8 lib/mocha/any_instance_method.rb
mocha-0.9.6 lib/mocha/any_instance_method.rb