Skip to content

Instantly share code, notes, and snippets.

@alecmunro
Created August 17, 2011 02:15
Show Gist options
  • Save alecmunro/1150664 to your computer and use it in GitHub Desktop.
Save alecmunro/1150664 to your computer and use it in GitHub Desktop.
Verifying mock expectations
"""Mocker:
Setting expectations with Mocker is as simple as calling the
methods on the mocks, and then switching the mocker instance
into replay mode."""
from mocker import Mocker
def mocker_test():
mocker = Mocker()
a_mock = mocker.mock()
a_mock.do_something()
mocker.replay()
a_mock.do_something()
mocker.verify() #Unnecessary within MockerTestCases
"""FlexMock:
Setting expectations with FlexMock is quite straightforward, as
long as you are using a test runner that FlexMock integrates
with, otherwise you need to verify manually."""
from flexmock import flexmock
def flexmock_test():
a_mock = flexmock()
expectation = a_mock.should_receive("do_something").once
#once is necessary to generate an expectation
a_mock.do_something()
expectation.verify()#Usually done automatically
"""Fudge:
fudge.verify() can be used to check whether mocks have been used
as expected. When used within a decorated (@patch or @test)
method, this is called automatically.
"""
import fudge
def fudge_test():
stub = fudge.Fake()
stub.expects("do_something")
stub.do_something()
fudge.verify() #Implicit within a decorated method.
"""Mock:
Mock supports verifying expectations by interrogating the mocks
afterwards. This can be rather verbose, but it does force you to
take verification seriously, which is good."""
import mock
def mock_test():
a_mock = mock.Mock()
a_mock.do_something()
a_mock.do_something.assert_called_once_with()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment