Class: Object

Inherits:
BasicObject
Includes:
Minitest::Expectations
Defined in:
lib/minitest/mock.rb,
lib/minitest/spec.rb

Overview

:nodoc:

Instance Method Summary collapse

Methods included from Minitest::Expectations

#assert_empty, #assert_equal, #assert_in_delta, #assert_in_epsilon, #assert_includes, #assert_instance_of, #assert_kind_of, #assert_match, #assert_nil, #assert_operator, #assert_output, #assert_path_exists, #assert_pattern, #assert_raises, #assert_respond_to, #assert_same, #assert_silent, #assert_throws, #refute_empty, #refute_equal, #refute_in_delta, #refute_in_epsilon, #refute_includes, #refute_instance_of, #refute_kind_of, #refute_match, #refute_nil, #refute_operator, #refute_path_exists, #refute_pattern, #refute_respond_to, #refute_same

Instance Method Details

#stub(name, val_or_callable, *block_args, **block_kwargs, &block) ⇒ Object

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

– NOTE: keyword args in callables are NOT checked for correctness against the existing method. Too many edge cases to be worth it.



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/minitest/mock.rb', line 280

def stub name, val_or_callable, *block_args, **block_kwargs, &block
  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, **kwargs|
      super(*args, **kwargs)
    end
  end

  metaclass.send :alias_method, new_name, name

  if ENV["MT_KWARGS_HAC\K"] then
    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, **block_kwargs) if blk
        val_or_callable
      end
    end
  else
    metaclass.send :define_method, name do |*args, **kwargs, &blk|
      if val_or_callable.respond_to? :call then
        if kwargs.empty? then # FIX: drop this after 2.7 dead
          val_or_callable.call(*args, &blk)
        else
          val_or_callable.call(*args, **kwargs, &blk)
        end
      else
        if blk then
          if block_kwargs.empty? then # FIX: drop this after 2.7 dead
            blk.call(*block_args)
          else
            blk.call(*block_args, **block_kwargs)
          end
        end
        val_or_callable
      end
    end
  end

  block[self]
ensure
  metaclass.send :undef_method, name
  metaclass.send :alias_method, name, new_name
  metaclass.send :undef_method, new_name
end