Permalink
Browse files

Merge pull request #524 from tenderlove/mocking

eliminate global vars, warnings, and fix test time
  • Loading branch information...
2 parents 864bdea + 66a8b8b commit 4dbddcb73efeff658d5c6fc782766e7a08376649 @hone hone committed Feb 28, 2012
Showing with 20 additions and 12 deletions.
  1. +6 −4 test/test_helper.rb
  2. +14 −8 test/worker_test.rb
View
10 test/test_helper.rb
@@ -147,12 +147,14 @@ def with_failure_backend(failure_backend, &block)
class Time
# Thanks, Timecop
class << self
+ attr_accessor :fake_time
+
alias_method :now_without_mock_time, :now
- def now_with_mock_time
- $fake_time || now_without_mock_time
+ def now
+ fake_time || now_without_mock_time
end
-
- alias_method :now, :now_with_mock_time
end
+
+ self.fake_time = nil
end
View
22 test/worker_test.rb
@@ -355,16 +355,22 @@ def self.exception
end
test "very verbose works in the afternoon" do
- require 'time'
- $last_puts = ""
- $fake_time = Time.parse("15:44:33 2011-03-02")
- singleton = class << @worker; self end
- singleton.send :define_method, :puts, lambda { |thing| $last_puts = thing }
+ begin
+ require 'time'
+ last_puts = ""
+ Time.fake_time = Time.parse("15:44:33 2011-03-02")
- @worker.very_verbose = true
- @worker.log("some log text")
+ @worker.extend(Module.new {
+ define_method(:puts) { |thing| last_puts = thing }
+ })
- assert_match /\*\* \[15:44:33 2011-03-02\] \d+: some log text/, $last_puts
+ @worker.very_verbose = true
+ @worker.log("some log text")
+
+ assert_match /\*\* \[15:44:33 2011-03-02\] \d+: some log text/, last_puts
+ ensure
+ Time.fake_time = nil
+ end
end
test "Will call an after_fork hook after forking" do

0 comments on commit 4dbddcb

Please sign in to comment.