Skip to content

Commit 0105c9a

Browse files
author
Florian Kromer
committed
add: implementation of setter injection (refactored from parameter injection)
1 parent 30bf0a8 commit 0105c9a

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

dft/parameter_injection.py renamed to dft/setter_injection.py

Lines changed: 6 additions & 3 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 "Parameter Injection" in
6+
Port of the Java example of "Setter 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.
@@ -26,8 +26,11 @@ class TimeDisplay(object):
2626
def __init__(self):
2727
pass
2828

29-
def get_current_time_as_html_fragment(self, time_provider):
30-
current_time = time_provider.now()
29+
def set_time_provider(self, time_provider):
30+
self.time_provider = time_provider
31+
32+
def get_current_time_as_html_fragment(self):
33+
current_time = self.time_provider.now()
3134
current_time_as_html_fragment = "<span class=\"tinyBoldText\">" + current_time + "</span>"
3235
return current_time_as_html_fragment
3336

tests/test_parameter_injection.py renamed to tests/test_setter_injection.py

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

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

77
"""
8-
Port of the Java example of "Parameter Injection" in
8+
Port of the Java example of "Setter 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.
@@ -29,8 +29,9 @@ def test_display_current_time_at_midnight(self):
2929
"""
3030
time_provider_stub = MidnightTimeProvider()
3131
class_under_test = TimeDisplay()
32+
class_under_test.set_time_provider(time_provider_stub)
3233
expected_time = "<span class=\"tinyBoldText\">24:01</span>"
33-
self.assertEqual(class_under_test.get_current_time_as_html_fragment(time_provider_stub), expected_time)
34+
self.assertEqual(class_under_test.get_current_time_as_html_fragment(), expected_time)
3435

3536
def test_display_current_time_at_current_time(self):
3637
"""
@@ -39,6 +40,7 @@ def test_display_current_time_at_current_time(self):
3940
"""
4041
production_code_time_provider = ProductionCodeTimeProvider()
4142
class_under_test = TimeDisplay()
43+
class_under_test.set_time_provider(production_code_time_provider)
4244
current_time = datetime.datetime.now()
4345
expected_time = "<span class=\"tinyBoldText\">" + str(current_time.hour) + ":" + str(current_time.minute) + "</span>"
44-
self.assertEqual(class_under_test.get_current_time_as_html_fragment(production_code_time_provider), expected_time)
46+
self.assertEqual(class_under_test.get_current_time_as_html_fragment(), expected_time)

0 commit comments

Comments
 (0)