module Minitest::Moar::Stubbing

Public Instance Methods

__stub__(obj, name, val_or_callable = nil, *block_args) { |obj| ... } click to toggle source
# File lib/minitest/moar/stubbing.rb, line 25
def __stub__ obj, name, val_or_callable = nil, *block_args, &block
  new_name = "__minitest_stub__#{name}"

  @invocations ||= Hash.new { |h, k| h[k] = Hash.new { |_h, _k| _h[_k] = 0 } }

  invoker = Proc.new { |klass, name| @invocations[klass][name] += 1 }

  obj.send :alias_method, new_name, name

  # Module.method(:define_method).bind(klass).call(name) do |*args, &blk|
  obj.send :define_method, name do |*args, &blk|
    if ::Class === self
      invoker.call(self, name)
    else
      klass = Kernel.instance_method(:class).bind(self).call
      invoker.call("Instance of #{klass}", name)
    end

    ret = if val_or_callable.respond_to? :call then
      val_or_callable.call(*args)
    else
      val_or_callable
    end

    blk.call(*block_args) if blk

    ret
  end

  yield obj
ensure
  obj.send :undef_method, name
  obj.send :alias_method, name, new_name
  obj.send :undef_method, new_name
end
instance_stub(obj, name, val_or_callable = nil, *block_args, &block) click to toggle source
Calls superclass method
# File lib/minitest/moar/stubbing.rb, line 15
def instance_stub obj, name, val_or_callable = nil, *block_args, &block
  if obj.respond_to? name and not obj.methods.map(&:to_s).include? name.to_s then
    obj.send :define_method, name do |*args|
      super(*args)
    end
  end

  __stub__ obj, name, val_or_callable, block_args, &block
end
stub(klass, name, val_or_callable = nil, *block_args, &block) click to toggle source
Calls superclass method
# File lib/minitest/moar/stubbing.rb, line 2
def stub klass, name, val_or_callable = nil, *block_args, &block
  @invocations ||= Hash.new { |h, k| h[k] = Hash.new { |_h, _k| _h[_k] = 0 } }

  metaclass = class << klass; self; end

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