Class: Object

Inherits:
BasicObject
Defined in:
lib/rubygems/test_case.rb

Overview

Instance Method Summary collapse

Instance Method Details

#stub(name, val_or_callable, *block_args) ⇒ Object



1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
# File 'lib/rubygems/test_case.rb', line 1578

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
    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
      val_or_callable.call(*args, &blk)
    else
      blk.call(*block_args) if blk
      val_or_callable
    end
  end

  metaclass.send(:ruby2_keywords, name) if metaclass.respond_to?(:ruby2_keywords, true)

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