vandrijevik-with_time_frozen_at 1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,22 @@
1
+ = HasPhoneNumber
2
+
3
+ with_time_frozen at is a testing helper that makes time-sensitive testing easy!
4
+
5
+ == How, you ask?
6
+
7
+ class TimeSensitiveTest < Test::Unit::TestCase
8
+ def test_that_something_happens_at_this_exact_time
9
+ expected_time = Time.local(1984, 5, 15)
10
+
11
+ with_time_frozen_at expected_time do |time|
12
+ assert_equal expected_time, Time.now
13
+ assert_equal expected_time, time
14
+ end
15
+ end
16
+ end
17
+
18
+ Inside the block you pass to with_time_frozen_at, Time.now returns the time that you specified in the call. This time is also passed as a block argument for your convenience.
19
+
20
+ == Credits
21
+
22
+ This version of with_time_frozen_at was inspired by the variant that was written by the authors in 2008. That implementation may have been slightly different and depended on Rails. This one does not.
@@ -0,0 +1,16 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = %q{with_time_frozen_at}
5
+ spec.version = "1.0"
6
+
7
+ spec.authors = ["Vladimir Andrijevik", "Dan Hodos"]
8
+ spec.date = %q{2009-06-10}
9
+ spec.description = %q{with_time_frozen_at makes writing time-sensitive tests in Ruby super easy.}
10
+ spec.email = %q{vladimir@andrijevik.net}
11
+ spec.files = ["README.rdoc", "with_time_frozen_at.gemspec", "with_time_frozen_at.rb"]
12
+ spec.homepage = %q{http://github.com/vandrijevik/with_time_frozen_at/}
13
+ spec.require_path = "."
14
+ spec.summary = %q{a helper for writing time-sensitive tests in Ruby}
15
+ spec.test_files = ["with_time_frozen_at_test.rb"]
16
+ end
@@ -0,0 +1,24 @@
1
+ module WithTimeFrozenAt
2
+ def with_time_frozen_at(desired_time, &block)
3
+ Time.class_eval do
4
+ (class << self; self; end).class_eval do
5
+ define_method :now_with_freeze do
6
+ desired_time
7
+ end
8
+ alias_method :now_without_freeze, :now
9
+ alias_method :now, :now_with_freeze
10
+ end
11
+ end
12
+ block.call(desired_time)
13
+ ensure
14
+ Time.class_eval do
15
+ (class << self; self; end).class_eval do
16
+ alias_method :now, :now_without_freeze
17
+ undef_method :now_without_freeze
18
+ undef_method :now_with_freeze
19
+ end
20
+ end
21
+ end
22
+ end # WithTimeFrozenAt
23
+
24
+ Test::Unit::TestCase.send(:include, WithTimeFrozenAt)
@@ -0,0 +1,44 @@
1
+ require 'test/unit'
2
+ require 'with_time_frozen_at'
3
+
4
+ class WithTimeFrozenAtTestCase < Test::Unit::TestCase
5
+ def test_calls_specified_block
6
+ assert_raises RuntimeError do
7
+ with_time_frozen_at Time.local(2009) do |time|
8
+ raise RuntimeError.new("Testing that this is called")
9
+ end
10
+ end
11
+ end
12
+
13
+ def test_yields_specified_time_as_a_block_parameter
14
+ expected_time = Time.local(2009, 6, 10, 22, 19)
15
+
16
+ with_time_frozen_at expected_time do |time|
17
+ assert_equal expected_time, time
18
+ end
19
+ end
20
+
21
+ def test_defines_now_with_freeze_method_on_time
22
+ with_time_frozen_at Time.local(2009) do |time|
23
+ assert_respond_to Time, :now_with_freeze
24
+ end
25
+ end
26
+
27
+ def test_defines_now_without_freeze_method_on_time
28
+ with_time_frozen_at Time.local(2009) do |time|
29
+ assert_respond_to Time, :now_without_freeze
30
+ end
31
+ end
32
+
33
+ def test_undefines_now_with_freeze_method_after_calling_the_block
34
+ with_time_frozen_at(Time.local(2009)) {}
35
+
36
+ assert !Time.respond_to?(:now_with_freeze)
37
+ end
38
+
39
+ def test_undefines_now_without_freeze_method_after_calling_the_block
40
+ with_time_frozen_at(Time.local(2009)) {}
41
+
42
+ assert !Time.respond_to?(:now_without_freeze)
43
+ end
44
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vandrijevik-with_time_frozen_at
3
+ version: !ruby/object:Gem::Version
4
+ version: "1.0"
5
+ platform: ruby
6
+ authors:
7
+ - Vladimir Andrijevik
8
+ - Dan Hodos
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-06-10 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: with_time_frozen_at makes writing time-sensitive tests in Ruby super easy.
18
+ email: vladimir@andrijevik.net
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - README.rdoc
27
+ - with_time_frozen_at.gemspec
28
+ - with_time_frozen_at.rb
29
+ has_rdoc: false
30
+ homepage: http://github.com/vandrijevik/with_time_frozen_at/
31
+ post_install_message:
32
+ rdoc_options: []
33
+
34
+ require_paths:
35
+ - .
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: "0"
41
+ version:
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ requirements: []
49
+
50
+ rubyforge_project:
51
+ rubygems_version: 1.2.0
52
+ signing_key:
53
+ specification_version: 2
54
+ summary: a helper for writing time-sensitive tests in Ruby
55
+ test_files:
56
+ - with_time_frozen_at_test.rb