Class: Object (Ruby 2.7.5)
Description: Class : Object - Ruby 2.7.5

### Home Classes Methods

### In Files

- minitest-5.13.0/lib/minitest/mock.rb
- minitest-5.13.0/lib/minitest/spec.rb
- minitest-5.13.0/lib/minitest/unit.rb

### Parent

BasicObject

### Methods

- #stub

### Files

- History.rdoc
- README.rdoc

### Class/Module Index

Quicksearch

- Kernel
- Minitest::AbstractReporter
- Minitest::Assertion
- Minitest::Assertions
- Minitest::BacktraceFilter
- Minitest::BenchSpec
- Minitest::Benchmark
- Minitest::CompositeReporter
- Minitest::Expectations
- Minitest::Guard
- Minitest::Mock
- Minitest::Parallel
- Minitest::Parallel::Executor
- Minitest::PrideIO
- Minitest::PrideLOL
- Minitest::ProgressReporter
- Minitest::Reportable
- Minitest::Result
- Minitest::Skip
- Minitest::Spec
- Minitest::Spec::DSL
- Minitest::Spec::DSL::InstanceMethods
- Minitest::StatisticsReporter
- Minitest::SummaryReporter
- Minitest::Test
- Minitest::Test::LifecycleHooks
- Minitest::UnexpectedError
- Object
- SubThingySpec
- ThingySpec

No matching classes.

# Object

`Object` extensions for `Minitest::Mock`.

### Public Instance Methods

stub(name, val_or_callable, *block_args) click to toggle source

Add a temporary stubbed method replacing `name` for the duration of the `block`. If `val_or_callable` responds to call, then it returns the result of calling it, otherwise returns the value as-is. If stubbed method yields a block, `block_args` will be passed along. Cleans up the stub at the end of the `block`. The method `name` must exist before stubbing.

    def test_stale_eh
      obj_under_test = Something.new
      refute obj_under_test.stale?

      Time.stub :now, Time.at(0) do
        assert obj_under_test.stale?
      end
    end

                   # File minitest-5.13.0/lib/minitest/mock.rb, line 212
    def stub name, val_or_callable, *block_args
      new_name = "__minitest_stub__#{name}"

      metaclass = class << self; self; end

      if respond_to? name and not methods.map(&:to_s).include? name.to_s then
        metaclass.send :define_method, name do |*args|
          super(*args)
        end
      end

      metaclass.send :alias_method, new_name, name

      metaclass.send :define_method, name do |*args, &blk|
        if val_or_callable.respond_to? :call then
          val_or_callable.call(*args, &blk)
        else
          blk.call(*block_args) if blk
          val_or_callable
        end
      end

      yield self
    ensure
      metaclass.send :undef_method, name
      metaclass.send :alias_method, name, new_name
      metaclass.send :undef_method, new_name
    end