Skip to content

Instantly share code, notes, and snippets.

@dundee
Created June 23, 2015 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dundee/a6aca1d053194ca59a67 to your computer and use it in GitHub Desktop.
Save dundee/a6aca1d053194ca59a67 to your computer and use it in GitHub Desktop.
Dependency injection by using multiple inheritance
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
@dundee
Copy link
Author

dundee commented Jun 23, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment