Skip to content

Commit d1c4efe

Browse files
author
Florian Kromer
committed
add: constructor injection files on clean branch
1 parent 95756e6 commit d1c4efe

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

dft/constructor_injection.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/python
2+
# -*- coding : utf-8 -*-
3+
import datetime
4+
5+
"""
6+
Port of the Java example of "Constructor Injection" in
7+
"xUnit Test Patterns - Refactoring Test Code" by Gerard Meszaros
8+
(ISBN-10: 0131495054, ISBN-13: 978-0131495050)
9+
10+
production code which is untestable:
11+
12+
class TimeDisplay(object):
13+
14+
def __init__(self):
15+
self.time_provider = datetime.datetime
16+
17+
def get_current_time_as_html_fragment(self):
18+
current_time = self.time_provider.now()
19+
current_time_as_html_fragment = "<span class=\"tinyBoldText\">{}</span>".format(current_time)
20+
return current_time_as_html_fragment
21+
"""
22+
23+
class TimeDisplay(object):
24+
25+
def __init__(self, time_provider):
26+
self.time_provider = time_provider
27+
28+
def get_current_time_as_html_fragment(self):
29+
current_time = self.time_provider.now()
30+
current_time_as_html_fragment = "<span class=\"tinyBoldText\">{}</span>".format(current_time)
31+
return current_time_as_html_fragment
32+
33+
class ProductionCodeTimeProvider(object):
34+
"""
35+
Production code version of the time provider (just a wrapper for formatting
36+
datetime for this example).
37+
"""
38+
39+
def now(self):
40+
current_time = datetime.datetime.now()
41+
current_time_formatted = "{}:{}".format(current_time.hour, current_time.minute)
42+
return current_time_formatted
43+
44+
class MidnightTimeProvider(object):
45+
"""
46+
Class implemented as hard-coded stub (in contrast to configurable stub).
47+
"""
48+
49+
def now(self):
50+
current_time_is_always_midnight = "24:01"
51+
return current_time_is_always_midnight

tests/test_constructor_injection.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
import unittest
4+
5+
from dft.constructor_injection import TimeDisplay, MidnightTimeProvider, ProductionCodeTimeProvider, datetime
6+
7+
"""
8+
Port of the Java example of "Constructor Injection" in
9+
"xUnit Test Patterns - Refactoring Test Code" by Gerard Meszaros
10+
(ISBN-10: 0131495054, ISBN-13: 978-0131495050)
11+
12+
Test code which will almost always fail (if not exactly 12:01) when untestable
13+
production code (production code time provider is datetime) is used:
14+
15+
def test_display_current_time_at_midnight(self):
16+
class_under_test = TimeDisplay()
17+
expected_time = "24:01"
18+
result = class_under_test.get_current_time_as_as_html_fragment()
19+
self.assertEqual(result, expected_time)
20+
"""
21+
22+
class ConstructorInjectionTest(unittest.TestCase):
23+
24+
def test_display_current_time_at_midnight(self):
25+
"""
26+
Will almost always fail (despite of right at/after midnight).
27+
"""
28+
time_provider_stub = MidnightTimeProvider()
29+
class_under_test = TimeDisplay(time_provider_stub)
30+
expected_time = "<span class=\"tinyBoldText\">24:01</span>"
31+
self.assertEqual(class_under_test.get_current_time_as_html_fragment(), expected_time)
32+
33+
def test_display_current_time_at_current_time(self):
34+
"""
35+
Just as justification for working example. (Will always pass.)
36+
"""
37+
production_code_time_provider = ProductionCodeTimeProvider()
38+
class_under_test = TimeDisplay(production_code_time_provider)
39+
current_time = datetime.datetime.now()
40+
expected_time = "<span class=\"tinyBoldText\">{}:{}</span>".format(current_time.hour, current_time.minute)
41+
self.assertEqual(class_under_test.get_current_time_as_html_fragment(), expected_time)
42+

0 commit comments

Comments
 (0)