Skip to content

Instantly share code, notes, and snippets.

@lukaszb
Created October 16, 2013 11:06
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 lukaszb/7006093 to your computer and use it in GitHub Desktop.
Save lukaszb/7006093 to your computer and use it in GitHub Desktop.
How to ensure method calls order
class Handler:
def handle(self):
self.process()
self.postprocess()
def process(self):
pass
def postprocess(self):
pass
handler = Handler()
"""
Problem: how to mock things so I can ensure that if I call
handler.handle()
the handler.process is called *before* handler.postprocess ?
"""
@lukaszb
Copy link
Author

lukaszb commented Oct 16, 2013

Oh, so the solution is to use mock_calls.

mocked = mock.Mock()

handler = Handler()
handler.process = mocked.process
handler.postprocess = mocked.postprocess

handler.handle()

assert mocked.mock_calls == [mock.call.process(), mock.call.postprocess()]

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