Skip to content

Commit 4dbddcb

Browse files
committed
Merge pull request resque#524 from tenderlove/mocking
eliminate global vars, warnings, and fix test time
2 parents 864bdea + 66a8b8b commit 4dbddcb

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

test/test_helper.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,14 @@ def with_failure_backend(failure_backend, &block)
147147
class Time
148148
# Thanks, Timecop
149149
class << self
150+
attr_accessor :fake_time
151+
150152
alias_method :now_without_mock_time, :now
151153

152-
def now_with_mock_time
153-
$fake_time || now_without_mock_time
154+
def now
155+
fake_time || now_without_mock_time
154156
end
155-
156-
alias_method :now, :now_with_mock_time
157157
end
158+
159+
self.fake_time = nil
158160
end

test/worker_test.rb

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -355,16 +355,22 @@ def self.exception
355355
end
356356

357357
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")
363362

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+
})
366366

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
368374
end
369375

370376
test "Will call an after_fork hook after forking" do

0 commit comments

Comments
 (0)