Skip to content
This repository
Browse code

+ Added Object#stub (in minitest/mock.rb).

[git-p4: depot-paths = "//src/minitest/dev/": change = 7432]
commit 37e1a04573f1047a1772a21cbfe48823d2c27d7e 1 parent 7ef920c
Ryan Davis authored May 08, 2012
1  .autotest
@@ -10,6 +10,7 @@ Autotest.add_hook :initialize do |at|
10 10
   at.extra_class_map["TestMeta"]                 = "test/test_minitest_spec.rb"
11 11
   at.extra_class_map["TestMiniTestUnitTestCase"] = "test/test_minitest_unit.rb"
12 12
   at.extra_class_map["TestMiniTestUnit"]         = "test/test_minitest_unit.rb"
  13
+  at.extra_class_map["TestMiniTestStub"]         = "test/test_minitest_mock.rb"
13 14
   at.add_exception 'coverage.info'
14 15
   at.add_exception 'coverage'
15 16
 end
18  README.txt
@@ -33,8 +33,8 @@ algorithms in a repeatable manner. Now you can assert that your newb
33 33
 co-worker doesn't replace your linear algorithm with an exponential
34 34
 one!
35 35
 
36  
-minitest/mock by Steven Baker, is a beautifully tiny mock object
37  
-framework.
  36
+minitest/mock by Steven Baker, is a beautifully tiny mock (and stub)
  37
+object framework.
38 38
 
39 39
 minitest/pride shows pride in testing and adds coloring to your test
40 40
 output. I guess it is an example of how to write IO pipes too. :P
@@ -54,7 +54,7 @@ discovery.
54 54
 * minitest/autorun - the easy and explicit way to run all your tests.
55 55
 * minitest/unit - a very fast, simple, and clean test system.
56 56
 * minitest/spec - a very fast, simple, and clean spec system.
57  
-* minitest/mock - a simple and clean mock system.
  57
+* minitest/mock - a simple and clean mock/stub system.
58 58
 * minitest/benchmark - an awesome way to assert your algorithm's performance.
59 59
 * minitest/pride - show your pride in testing!
60 60
 * Incredibly small and fast runner, but no bells and whistles.
@@ -194,6 +194,18 @@ Output is tab-delimited to make it easy to paste into a spreadsheet.
194 194
     end
195 195
   end
196 196
 
  197
+=== Stubs
  198
+
  199
+  def test_stale_eh
  200
+    obj_under_test = Something.new
  201
+
  202
+    refute obj_under_test.stale?
  203
+
  204
+    Time.stub :now, Time.at(0) do   # stub goes away once the block is done
  205
+      assert obj_under_test.stale?
  206
+    end
  207
+  end
  208
+
197 209
 === Customizable Test Runner Types:
198 210
 
199 211
 MiniTest::Unit.runner=(runner) provides an easy way of creating custom
38  lib/minitest/mock.rb
@@ -125,3 +125,41 @@ def respond_to?(sym) # :nodoc:
125 125
     end
126 126
   end
127 127
 end
  128
+
  129
+class Object # :nodoc:
  130
+
  131
+  ##
  132
+  # Add a temporary stubbed method replacing +name+ for the duration
  133
+  # of the +block+. If +val_or_callable+ responds to #call, then it
  134
+  # returns the result of calling it, otherwise returns the value
  135
+  # as-is. Cleans up the stub at the end of the +block+.
  136
+  #
  137
+  #     def test_stale_eh
  138
+  #       obj_under_test = Something.new
  139
+  #       refute obj_under_test.stale?
  140
+  #
  141
+  #       Time.stub :now, Time.at(0) do
  142
+  #         assert obj_under_test.stale?
  143
+  #       end
  144
+  #     end
  145
+
  146
+  def stub name, val_or_callable, &block
  147
+    new_name = "__minitest_stub__#{name}"
  148
+
  149
+    metaclass = class << self; self; end
  150
+    metaclass.send :alias_method, new_name, name
  151
+    metaclass.send :define_method, name do |*args|
  152
+      if val_or_callable.respond_to? :call then
  153
+        val_or_callable.call(*args)
  154
+      else
  155
+        val_or_callable
  156
+      end
  157
+    end
  158
+
  159
+    yield
  160
+  ensure
  161
+    metaclass.send :undef_method, name
  162
+    metaclass.send :alias_method, name, new_name
  163
+    metaclass.send :undef_method, new_name
  164
+  end
  165
+end
59  test/minitest/test_minitest_mock.rb
@@ -203,3 +203,62 @@ def util_verify_bad exp
203 203
     assert_equal exp, e.message
204 204
   end
205 205
 end
  206
+
  207
+require "test/minitest/metametameta"
  208
+
  209
+class TestMiniTestStub < MiniTest::Unit::TestCase
  210
+  def setup
  211
+    super
  212
+    MiniTest::Unit::TestCase.reset
  213
+
  214
+    @tc = MiniTest::Unit::TestCase.new 'fake tc'
  215
+    @assertion_count = 1
  216
+  end
  217
+
  218
+  def teardown
  219
+    super
  220
+    assert_equal @assertion_count, @tc._assertions
  221
+  end
  222
+
  223
+  def assert_stub val_or_callable
  224
+    @assertion_count += 1
  225
+
  226
+    t = Time.now.to_i
  227
+
  228
+    Time.stub :now, val_or_callable do
  229
+      @tc.assert_equal 42, Time.now
  230
+    end
  231
+
  232
+    @tc.assert_operator Time.now.to_i, :>=, t
  233
+  end
  234
+
  235
+  def test_stub_value
  236
+    assert_stub 42
  237
+  end
  238
+
  239
+  def test_stub_block
  240
+    assert_stub lambda { 42 }
  241
+  end
  242
+
  243
+  def test_stub_block_args
  244
+    @assertion_count += 1
  245
+
  246
+    t = Time.now.to_i
  247
+
  248
+    Time.stub :now,  lambda { |n| n * 2 } do
  249
+      @tc.assert_equal 42, Time.now(21)
  250
+    end
  251
+
  252
+    @tc.assert_operator Time.now.to_i, :>=, t
  253
+  end
  254
+
  255
+  def test_stub_callable
  256
+    obj = Object.new
  257
+
  258
+    def obj.call
  259
+      42
  260
+    end
  261
+
  262
+    assert_stub obj
  263
+  end
  264
+end

2 notes on commit 37e1a04

Carlos León

Awesome. Thank you very much for this.

Guillermo Iguaran

Thanks for this :heart:

Please sign in to comment.
Something went wrong with that request. Please try again.