77"""
88Port of the Java example of "Constructor Injection" in
99"xUnit Test Patterns - Refactoring Test Code" by Gerard Meszaros
10- (ISBN-10: 0131495054, ISBN-13: 978-0131495050)
10+ (ISBN-10: 0131495054, ISBN-13: 978-0131495050) accessible in outdated version on
11+ http://xunitpatterns.com/Dependency%20Injection.html.
1112
1213Test 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+ production code (have a look into constructor_injection.py ) is used:
1415
1516 def test_display_current_time_at_midnight(self):
1617 class_under_test = TimeDisplay()
@@ -23,7 +24,8 @@ class ConstructorInjectionTest(unittest.TestCase):
2324
2425 def test_display_current_time_at_midnight (self ):
2526 """
26- Will almost always fail (despite of right at/after midnight).
27+ Would almost always fail (despite of right at/after midnight) if
28+ untestable production code would have been used.
2729 """
2830 time_provider_stub = MidnightTimeProvider ()
2931 class_under_test = TimeDisplay (time_provider_stub )
@@ -32,16 +34,11 @@ def test_display_current_time_at_midnight(self):
3234
3335 def test_display_current_time_at_current_time (self ):
3436 """
35- Just as justification for working example. (Will always pass.)
37+ Just as justification for working example with the time provider used in
38+ production. (Will always pass.)
3639 """
3740 production_code_time_provider = ProductionCodeTimeProvider ()
3841 class_under_test = TimeDisplay (production_code_time_provider )
3942 current_time = datetime .datetime .now ()
4043 expected_time = "<span class=\" tinyBoldText\" >" + str (current_time .hour ) + ":" + str (current_time .minute ) + "</span>"
4144 self .assertEqual (class_under_test .get_current_time_as_html_fragment (), expected_time )
42-
43- if __name__ == "__main__" :
44- # PYTHONPATH=$PYTHONPATH:~/ws_github/python-patterns/
45- # export PYTHONPATH
46- # python tests/test_constructor_injection.py
47- unittest .main ()
0 commit comments