New submission from Mario Corchero:

Define a way to disable the automatic generation of submocks when accessing an 
attribute of a mock which is not set.


Rationale:
Inspired by GMock RestrictedMock, it aims to allow the developer to declare a 
narrow interface to the mock that defines what the mocks allows to be called on.
The feature of mocks returning mocks by default is extremely useful but not 
always desired. Quite often you rely on it only at the time you are writing the 
test but you want it to be disabled at the time the mock is passed into your 
code.

It also prevents user errors when mocking incorrect paths or having typos when 
calling attributes/methods of the mock.
We have tried it internally in our company and it gives quite a nicer user 
experience for many use cases, specially for new users of mock as it helps out 
when you mock the wrong path.

Posible interfaces:
New Mock type, SeledMock which can be used instead of the "common" mock that 
has an attribute "sealed" which once set to true disables the dynamic 
generation of "submocks"


The final goal is to be able to write tests like:

>>> m = mock.Mock()  # or = mock.SealableMock()
>>> m.method1.return_value.attr1.method2.return_value = 1
>>> mock.seal(m)  # or mock.sealed = True

>>> m.method1().attr1.method2()  # This path has been declared above
# 1
>>> m.method1().attr2  # This was not defined so it is going to raise a 
>>> meaningful exception
# Exception: SealedMockAttributeAccess: mock.method1().attr2

----------
components: Library (Lib)
messages: 294961
nosy: Mario Corchero, haypo
priority: normal
severity: normal
status: open
title: Add restricted mocks to the python unittest mocking framework
type: enhancement
versions: Python 3.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue30541>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to