Created
September 8, 2011 16:20
-
-
Save alecmunro/1203804 to your computer and use it in GitHub Desktop.
Chaining methods and attributes
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
"""Mocker: | |
Chaining attribute or method access in Mocker is trivial. During | |
the record phase, every mock operation returns a mock.""" | |
from mocker import Mocker | |
def mocker_test(): | |
mocker = Mocker() | |
a_mock = mocker.mock() | |
a_mock.clock().block.shock() | |
mocker.replay() | |
a_mock.clock().block.shock() | |
"""Flexmock: | |
Flexmock has a shortcut for simple chained method calls, but a | |
verbose syntax for chained attributes, or complex method calls. | |
""" | |
from flexmock import flexmock | |
def flexmock_test_attributes(): | |
a_mock = flexmock(clock=flexmock(block=flexmock( | |
shock=lambda:None))) | |
a_mock.clock.block.shock() | |
def flexmock_test_methods(): | |
a_mock = flexmock() | |
a_mock.should_receive("clock.block.shock") | |
a_mock.clock().block().shock() | |
def flexmock_test_methods_complex(): | |
a_mock = flexmock() | |
a_mock.should_receive("clock").with_args("a").and_return( | |
flexmock().should_receive("block").with_args("b")\ | |
.and_return(flexmock().should_receive("shock").mock | |
).mock | |
) | |
a_mock.clock("a").block("b").shock() | |
"""Fudge: | |
Fudge has a somewhat verbose method of dealing with chained | |
methods and attributes.""" | |
import fudge | |
def fudge_test(): | |
a_mock = fudge.Fake() | |
a_mock.provides("clock").returns_fake().has_attr( | |
block=fudge.Fake().provides("shock")) | |
a_mock.clock().block.shock() | |
"""Mock: | |
Mock provides a very concise syntax for chaining methods and | |
attributes. | |
""" | |
import mock | |
def mock_test(): | |
a_mock = mock.Mock() | |
m_shock = a_mock.clock.return_value.block.shock | |
a_mock.clock().block.shock() | |
m_shock.assert_called_once_with() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment