Skip to content

Commit b14a654

Browse files
committed
Merge branch 'fkromer-parameter_injection'
2 parents d3ea46e + 79c88e4 commit b14a654

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ __Behavioral Patterns__:
5656
| [template](behavioral/template.py) | an object imposes a structure but takes pluggable components |
5757
| [visitor](behavioral/visitor.py) | invoke a callback for all items of a collection |
5858

59+
__Design for Testability Patterns__:
60+
61+
| Pattern | Description |
62+
|:-------:| ----------- |
63+
| [constructor_injection](dft/constructor_injection.py) | the client provides the depended-on object to the SUT via the class construtor (implmentation variant of dependency injection) |
64+
| [parameter_injection](dft/parameter_injection.py) | the client provides the depended-on object to the SUT via a parameter(implmentation variant of dependency injection) |
65+
5966
__Fundamental Patterns__:
6067

6168
| Pattern | Description |

dft/constructor_injection.py renamed to dft/parameter_injection.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import datetime
44

55
"""
6-
Port of the Java example of "Constructor Injection" in
6+
Port of the Java example of "Parameter Injection" in
77
"xUnit Test Patterns - Refactoring Test Code" by Gerard Meszaros
88
(ISBN-10: 0131495054, ISBN-13: 978-0131495050) accessible in outdated version on
99
http://xunitpatterns.com/Dependency%20Injection.html.
@@ -23,11 +23,11 @@ def get_current_time_as_html_fragment(self):
2323

2424
class TimeDisplay(object):
2525

26-
def __init__(self, time_provider):
27-
self.time_provider = time_provider
26+
def __init__(self):
27+
pass
2828

29-
def get_current_time_as_html_fragment(self):
30-
current_time = self.time_provider.now()
29+
def get_current_time_as_html_fragment(self, time_provider):
30+
current_time = time_provider.now()
3131
current_time_as_html_fragment = "<span class=\"tinyBoldText\">" + current_time + "</span>"
3232
return current_time_as_html_fragment
3333

tests/test_constructor_injection.py renamed to tests/test_parameter_injection.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
# -*- coding: utf-8 -*-
33
import unittest
44

5-
from dft.constructor_injection import TimeDisplay, MidnightTimeProvider, ProductionCodeTimeProvider, datetime
5+
from dft.parameter_injection import TimeDisplay, MidnightTimeProvider, ProductionCodeTimeProvider, datetime
66

77
"""
8-
Port of the Java example of "Constructor Injection" in
8+
Port of the Java example of "Parameter Injection" in
99
"xUnit Test Patterns - Refactoring Test Code" by Gerard Meszaros
1010
(ISBN-10: 0131495054, ISBN-13: 978-0131495050) accessible in outdated version on
1111
http://xunitpatterns.com/Dependency%20Injection.html.
@@ -20,28 +20,25 @@ def test_display_current_time_at_midnight(self):
2020
self.assertEqual(result, expected_time)
2121
"""
2222

23-
class ConstructorInjectionTest(unittest.TestCase):
23+
class ParameterInjectionTest(unittest.TestCase):
2424

2525
def test_display_current_time_at_midnight(self):
2626
"""
2727
Would almost always fail (despite of right at/after midnight) if
2828
untestable production code would have been used.
2929
"""
3030
time_provider_stub = MidnightTimeProvider()
31-
class_under_test = TimeDisplay(time_provider_stub)
31+
class_under_test = TimeDisplay()
3232
expected_time = "<span class=\"tinyBoldText\">24:01</span>"
33-
self.assertEqual(class_under_test.get_current_time_as_html_fragment(), expected_time)
33+
self.assertEqual(class_under_test.get_current_time_as_html_fragment(time_provider_stub), expected_time)
3434

3535
def test_display_current_time_at_current_time(self):
3636
"""
3737
Just as justification for working example with the time provider used in
3838
production. (Will always pass.)
3939
"""
4040
production_code_time_provider = ProductionCodeTimeProvider()
41-
class_under_test = TimeDisplay(production_code_time_provider)
41+
class_under_test = TimeDisplay()
4242
current_time = datetime.datetime.now()
4343
expected_time = "<span class=\"tinyBoldText\">{}:{}</span>".format(current_time.hour, current_time.minute)
44-
self.assertEqual(class_under_test.get_current_time_as_html_fragment(), expected_time)
45-
46-
if __name__ == "__main__":
47-
unittest.main()
44+
self.assertEqual(class_under_test.get_current_time_as_html_fragment(production_code_time_provider), expected_time)

0 commit comments

Comments
 (0)