Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow passing mock name to mocker.stub() #40

Merged
merged 2 commits into from
May 25, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,15 @@ Stub
*New in version 0.6*

The stub is a mock object that accepts any arguments and is useful to test callbacks, for instance.
May be passed a name to be used by the constructed stub object in its repr (useful for debugging).

.. code-block:: python

def test_stub(mocker):
def foo(on_something):
on_something('foo', 'bar')

stub = mocker.stub()
stub = mocker.stub(name='on_something_stub')

foo(stub)
stub.assert_called_once_with('foo', 'bar')
Expand Down
5 changes: 3 additions & 2 deletions pytest_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,16 @@ def spy(self, obj, name):
autospec=autospec)
return result

def stub(self):
def stub(self, name=None):
"""
Creates a stub method. It accepts any arguments. Ideal to register to
callbacks in tests.

:param name: the constructed stub's name as used in repr
:rtype: mock.MagicMock
:return: Stub object.
"""
return mock_module.MagicMock(spec=lambda *args, **kwargs: None)
return mock_module.MagicMock(spec=lambda *args, **kwargs: None, name=name)

class _Patcher(object):
"""
Expand Down
37 changes: 29 additions & 8 deletions test_pytest_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,35 @@ def test_mocker_resetall(mocker):
assert not open.called


def test_mocker_stub(mocker):
def foo(on_something):
on_something('foo', 'bar')

stub = mocker.stub()

foo(stub)
stub.assert_called_once_with('foo', 'bar')
class TestMockerStub:
def test_call(self, mocker):
stub = mocker.stub()
stub('foo', 'bar')
stub.assert_called_once_with('foo', 'bar')

def test_repr_with_no_name(self, mocker):
stub = mocker.stub()
assert not 'name' in repr(stub)

def test_repr_with_name(self, mocker):
test_name = 'funny walk'
stub = mocker.stub(name=test_name)
assert "name={0!r}".format(test_name) in repr(stub)

def __test_failure_message(self, mocker, **kwargs):
expected_name = kwargs.get('name') or 'mock'
expected_message = 'Expected call: {0}()\nNot called'.format(expected_name)
stub = mocker.stub(**kwargs)
with pytest.raises(AssertionError) as exc_info:
stub.assert_called_with()
assert exc_info.value.msg == expected_message

def test_failure_message_with_no_name(self, mocker):
self.__test_failure_message(mocker)

@pytest.mark.parametrize('name', (None, '', 'f', 'The Castle of aaarrrrggh'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice reference 😉

def test_failure_message_with_name(self, mocker, name):
self.__test_failure_message(mocker, name=name)


def test_instance_method_spy(mocker):
Expand Down