Created
June 23, 2015 09:38
-
-
Save dundee/a6aca1d053194ca59a67 to your computer and use it in GitHub Desktop.
Dependency injection by using multiple inheritance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Robot(object): | |
def move_forward(): | |
print('Moving forward') | |
def move_backward(): | |
print('Moving backward') | |
class CleaningRobot(Robot): | |
def clean(self, times=10): | |
for i in range(times): | |
super().move_forward() | |
super().move_backward() | |
# --------------------------------- | |
class MockBot(Robot): | |
def __init__(self): | |
self.moves = [] | |
def move_forward(self): | |
self.moves.append('forward') | |
def move_backward(self): | |
self.moves.append('backward') | |
class MockedCleaningRobot(CleaningRobot, MockBot): | |
pass | |
def test_clean(): | |
t = MockedCleaningRobot() | |
t.clean(times=2) | |
expected = [ | |
'forward', 'backward', | |
'forward', 'backward', | |
] | |
assert t.moves == expected |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See https://www.youtube.com/watch?v=EiOglTERPEo