Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #524 from tenderlove/mocking

eliminate global vars, warnings, and fix test time
  • Loading branch information...
commit 4dbddcb73efeff658d5c6fc782766e7a08376649 2 parents 864bdea + 66a8b8b
Terence Lee authored February 28, 2012
10  test/test_helper.rb
@@ -147,12 +147,14 @@ def with_failure_backend(failure_backend, &block)
147 147
 class Time
148 148
   # Thanks, Timecop
149 149
   class << self
  150
+    attr_accessor :fake_time
  151
+
150 152
     alias_method :now_without_mock_time, :now
151 153
 
152  
-    def now_with_mock_time
153  
-      $fake_time || now_without_mock_time
  154
+    def now
  155
+      fake_time || now_without_mock_time
154 156
     end
155  
-
156  
-    alias_method :now, :now_with_mock_time
157 157
   end
  158
+
  159
+  self.fake_time = nil
158 160
 end
22  test/worker_test.rb
@@ -355,16 +355,22 @@ def self.exception
355 355
   end
356 356
 
357 357
   test "very verbose works in the afternoon" do
358  
-    require 'time'
359  
-    $last_puts = ""
360  
-    $fake_time = Time.parse("15:44:33 2011-03-02")
361  
-    singleton = class << @worker; self end
362  
-    singleton.send :define_method, :puts, lambda { |thing| $last_puts = thing }
  358
+    begin
  359
+      require 'time'
  360
+      last_puts = ""
  361
+      Time.fake_time = Time.parse("15:44:33 2011-03-02")
363 362
 
364  
-    @worker.very_verbose = true
365  
-    @worker.log("some log text")
  363
+      @worker.extend(Module.new {
  364
+        define_method(:puts) { |thing| last_puts = thing }
  365
+      })
366 366
 
367  
-    assert_match /\*\* \[15:44:33 2011-03-02\] \d+: some log text/, $last_puts
  367
+      @worker.very_verbose = true
  368
+      @worker.log("some log text")
  369
+
  370
+      assert_match /\*\* \[15:44:33 2011-03-02\] \d+: some log text/, last_puts
  371
+    ensure
  372
+      Time.fake_time = nil
  373
+    end
368 374
   end
369 375
 
370 376
   test "Will call an after_fork hook after forking" do

0 notes on commit 4dbddcb

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