Class: Mocha::Mockery

### Home Classes Methods

### In Files

- lib/mocha/mockery.rb

### Parent

Object

### Methods

- ::instance
- ::reset_instance
- #logger
- #mocha_inspect
- #mock_impersonating
- #mock_impersonating_any_instance_of
- #mocks
- #named_mock
- #new_state_machine
- #on_stubbing
- #on_stubbing_method_on_non_mock_object
- #on_stubbing_method_unnecessarily
- #on_stubbing_non_existent_method
- #on_stubbing_non_public_method
- #state_machines
- #stubba
- #teardown
- #unnamed_mock
- #verify

### 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::Mockery

### Attributes

logger[W]

### Public Class Methods

instance() click to toggle source

    # File lib/mocha/mockery.rb, line 15
    def instance
      @instance ||= new
    end

reset_instance() click to toggle source

    # File lib/mocha/mockery.rb, line 19
    def reset_instance
      @instance = nil
    end

### Public Instance Methods

logger() click to toggle source

    # File lib/mocha/mockery.rb, line 144
    def logger
      @logger ||= Logger.new($stderr)
    end

mocha_inspect() click to toggle source

    # File lib/mocha/mockery.rb, line 81
    def mocha_inspect
      message = ""
      message << "unsatisfied expectations:\n- #{unsatisfied_expectations.map { |e| e.mocha_inspect }.join("\n- ")}\n" unless unsatisfied_expectations.empty?
      message << "satisfied expectations:\n- #{satisfied_expectations.map { |e| e.mocha_inspect }.join("\n- ")}\n" unless satisfied_expectations.empty?
      message << "states:\n- #{state_machines.map { |sm| sm.mocha_inspect }.join("\n- ")}" unless state_machines.empty?
      message
    end

mock_impersonating(object, &block) click to toggle source

    # File lib/mocha/mockery.rb, line 33
    def mock_impersonating(object, &block)
      add_mock(Mock.new(ImpersonatingName.new(object), &block))
    end

mock_impersonating_any_instance_of(klass, &block) click to toggle source

    # File lib/mocha/mockery.rb, line 37
    def mock_impersonating_any_instance_of(klass, &block)
      add_mock(Mock.new(ImpersonatingAnyInstanceName.new(klass), &block))
    end

mocks() click to toggle source

    # File lib/mocha/mockery.rb, line 73
    def mocks
      @mocks ||= []
    end

named_mock(name, &block) click to toggle source

    # File lib/mocha/mockery.rb, line 25
    def named_mock(name, &block)
      add_mock(Mock.new(Name.new(name), &block))
    end

new_state_machine(name) click to toggle source

    # File lib/mocha/mockery.rb, line 41
    def new_state_machine(name)
      add_state_machine(StateMachine.new(name))
    end

on_stubbing(object, method) click to toggle source

    # File lib/mocha/mockery.rb, line 89
    def on_stubbing(object, method)
      method = RUBY_VERSION < '1.9' ? method.to_s : method.to_sym
      unless Mocha::Configuration.allow?(:stubbing_non_existent_method)
        unless object.method_exists?(method, include_public_methods = true)
          on_stubbing_non_existent_method(object, method)
        end
      end
      unless Mocha::Configuration.allow?(:stubbing_non_public_method)
        if object.method_exists?(method, include_public_methods = false)
          on_stubbing_non_public_method(object, method)
        end
      end
      unless Mocha::Configuration.allow?(:stubbing_method_on_non_mock_object)
        on_stubbing_method_on_non_mock_object(object, method)
      end
    end

on_stubbing_method_on_non_mock_object(object, method) click to toggle source

    # File lib/mocha/mockery.rb, line 124
    def on_stubbing_method_on_non_mock_object(object, method)
      if Mocha::Configuration.prevent?(:stubbing_method_on_non_mock_object)
        raise StubbingError.new("stubbing method on non-mock object: #{object.mocha_inspect}.#{method}", caller)
      end
      if Mocha::Configuration.warn_when?(:stubbing_method_on_non_mock_object)
        logger.warn "stubbing method on non-mock object: #{object.mocha_inspect}.#{method}"
      end
    end

on_stubbing_method_unnecessarily(expectation) click to toggle source

    # File lib/mocha/mockery.rb, line 133
    def on_stubbing_method_unnecessarily(expectation)
      if Mocha::Configuration.prevent?(:stubbing_method_unnecessarily)
        raise StubbingError.new("stubbing method unnecessarily: #{expectation.method_signature}", expectation.backtrace)
      end
      if Mocha::Configuration.warn_when?(:stubbing_method_unnecessarily)
        logger.warn "stubbing method unnecessarily: #{expectation.method_signature}"
      end
    end

on_stubbing_non_existent_method(object, method) click to toggle source

    # File lib/mocha/mockery.rb, line 106
    def on_stubbing_non_existent_method(object, method)
      if Mocha::Configuration.prevent?(:stubbing_non_existent_method)
        raise StubbingError.new("stubbing non-existent method: #{object.mocha_inspect}.#{method}", caller)
      end
      if Mocha::Configuration.warn_when?(:stubbing_non_existent_method)
        logger.warn "stubbing non-existent method: #{object.mocha_inspect}.#{method}"
      end
    end

on_stubbing_non_public_method(object, method) click to toggle source

    # File lib/mocha/mockery.rb, line 115
    def on_stubbing_non_public_method(object, method)
      if Mocha::Configuration.prevent?(:stubbing_non_public_method)
        raise StubbingError.new("stubbing non-public method: #{object.mocha_inspect}.#{method}", caller)
      end
      if Mocha::Configuration.warn_when?(:stubbing_non_public_method)
        logger.warn "stubbing non-public method: #{object.mocha_inspect}.#{method}"
      end
    end

state_machines() click to toggle source

    # File lib/mocha/mockery.rb, line 77
    def state_machines
      @state_machines ||= []
    end

stubba() click to toggle source

    # File lib/mocha/mockery.rb, line 69
    def stubba
      @stubba ||= Central.new
    end

teardown() click to toggle source

    # File lib/mocha/mockery.rb, line 64
    def teardown
      stubba.unstub_all
      reset
    end

unnamed_mock(&block) click to toggle source

    # File lib/mocha/mockery.rb, line 29
    def unnamed_mock(&block)
      add_mock(Mock.new(&block))
    end

verify(assertion_counter = nil) click to toggle source

    # File lib/mocha/mockery.rb, line 45
    def verify(assertion_counter = nil)
      unless mocks.all? { |mock| mock.__verified__?(assertion_counter) }
        message = "not all expectations were satisfied\n#{mocha_inspect}"
        if unsatisfied_expectations.empty?
          backtrace = caller
        else
          backtrace = unsatisfied_expectations[0].backtrace
        end
        raise ExpectationError.new(message, backtrace)
      end
      expectations.each do |e|
        unless Mocha::Configuration.allow?(:stubbing_method_unnecessarily)
          unless e.used?
            on_stubbing_method_unnecessarily(e)
          end
        end
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.