Class: Mocha::ClassMethod

### Home Classes Methods

### In Files

- lib/mocha/class_method.rb

### Parent

Object

### Methods

- ::new
- #==
- #define_new_method
- #eql?
- #hidden_method
- #hide_original_method
- #method_exists?
- #mock
- #remove_new_method
- #restore_original_method
- #stub
- #to_s
- #unstub

### Files

- COPYING
- README

### Class/Module Index

Quicksearch

- Mocha
- Mocha::API
- Mocha::AnyInstanceMethod
- Mocha::ArgumentIterator
- Mocha::ArrayMethods
- Mocha::BacktraceFilter
- Mocha::Cardinality
- Mocha::Central
- Mocha::ChangeStateSideEffect
- Mocha::ClassMethod
- Mocha::ClassMethods
- Mocha::Configuration
- Mocha::DateMethods
- Mocha::DefaultName
- Mocha::Deprecation
- Mocha::Expectation
- Mocha::ExpectationError
- Mocha::ExpectationList
- Mocha::HashMethods
- Mocha::ImpersonatingAnyInstanceName
- Mocha::ImpersonatingName
- Mocha::InStateOrderingConstraint
- Mocha::InstanceMethod
- Mocha::Integration
- Mocha::Integration::MiniTest
- Mocha::Integration::MiniTest::AssertionCounter
- Mocha::Integration::MiniTest::Version131AndAbove
- Mocha::Integration::TestUnit
- Mocha::Integration::TestUnit::AssertionCounter
- Mocha::Integration::TestUnit::GemVersion200
- Mocha::Integration::TestUnit::GemVersion201AndAbove
- Mocha::Integration::TestUnit::RubyVersion185AndBelow
- Mocha::Integration::TestUnit::RubyVersion186AndAbove
- Mocha::Logger
- Mocha::MethodMatcher
- Mocha::Mock
- Mocha::Mockery
- Mocha::ModuleMethod
- Mocha::Name
- Mocha::ObjectMethods
- Mocha::ParameterMatchers
- Mocha::ParametersMatcher
- Mocha::PrettyParameters
- Mocha::Sequence
- Mocha::Sequence::InSequenceOrderingConstraint
- Mocha::StateMachine
- Mocha::StringMethods
- Mocha::StubbingError
- Mocha::TimeMethods
- Mocha::UnexpectedInvocation
- MiniTest
- MiniTest::Unit
- MiniTest::Unit::TestCase
- Test
- Test::Unit
- Test::Unit::TestCase
- Array
- Date
- Hash
- Object
- String
- Time

No matching classes.

# 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
      @method = RUBY_VERSION < '1.9' ? method.to_s : method.to_sym
    end

### Public Instance Methods

==(other) click to toggle source
Alias for: eql?
define_new_method() click to toggle source

    # File lib/mocha/class_method.rb, line 39
    def define_new_method
      stubbee.__metaclass__.class_eval(%{
        def #{method}(*args, &block)
          mocha.method_missing(:#{method}, *args, &block)
        end
      }, __FILE__, __LINE__)
    end

eql?(other) click to toggle source

    # File lib/mocha/class_method.rb, line 72
    def eql?(other)
      return false unless (other.class == self.class)
      (stubbee.object_id == other.stubbee.object_id) and (method == other.method)
    end

Also aliased as: ==
hidden_method() click to toggle source

    # File lib/mocha/class_method.rb, line 62
    def hidden_method
      if RUBY_VERSION < '1.9'
        method_name = method.to_s.gsub(/\W/) { |s| "_substituted_character_#{s[0]}_" }
      else
        method_name = method.to_s.gsub(/\W/) { |s| "_substituted_character_#{s.ord}_" }
      end
      hidden_method = "__stubba__#{method_name}__stubba__"
      RUBY_VERSION < '1.9' ? hidden_method.to_s : hidden_method.to_sym
    end

hide_original_method() click to toggle source

    # File lib/mocha/class_method.rb, line 29
    def hide_original_method
      if method_exists?(method)
        begin
          stubbee.__metaclass__.send(:alias_method, hidden_method, method)
        rescue NameError
          # deal with nasties like ActiveRecord::Associations::AssociationProxy
        end
      end
    end

method_exists?(method) click to toggle source

    # File lib/mocha/class_method.rb, line 83
    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 25
    def mock
      stubbee.mocha
    end

remove_new_method() click to toggle source

    # File lib/mocha/class_method.rb, line 47
    def remove_new_method
      stubbee.__metaclass__.send(:remove_method, method)
    end

restore_original_method() click to toggle source

    # File lib/mocha/class_method.rb, line 51
    def restore_original_method
      if method_exists?(hidden_method)
        begin
          stubbee.__metaclass__.send(:alias_method, method, hidden_method)
          stubbee.__metaclass__.send(:remove_method, hidden_method)
        rescue NameError
          # deal with nasties like ActiveRecord::Associations::AssociationProxy
        end
      end
    end

stub() click to toggle source

    # File lib/mocha/class_method.rb, line 14
    def stub
      hide_original_method
      define_new_method
    end

to_s() click to toggle source

    # File lib/mocha/class_method.rb, line 79
    def to_s
      "#{stubbee}.#{method}"
    end

unstub() click to toggle source

    # File lib/mocha/class_method.rb, line 19
    def unstub
      remove_new_method
      restore_original_method
      stubbee.reset_mocha
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.