derwiki-adroll / mock

Automatically exported from code.google.com/p/mock
BSD 2-Clause "Simplified" License
0 stars 0 forks source link

Prevent non-existent assert methods being called #212

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
A regex to prevent the calling of non-existent assert / assret methods. With a 
way to switch off in case those methods aree actually needed.

Original issue reported on code.google.com by fuzzyman on 10 Aug 2013 at 7:03

GoogleCodeExporter commented 9 years ago
So:

    mock.assert_called_once()
    mock.assret_called_once_with()

would then raise AttributeError instead of silently working.

Original comment by fuzzyman on 10 Aug 2013 at 7:04

GoogleCodeExporter commented 9 years ago
(The regex would be in attribute lookup.)

I *am* proposing it be on by default.

Original comment by fuzzyman on 10 Aug 2013 at 7:07

GoogleCodeExporter commented 9 years ago
Yes, please. Have bitten by it multiple times..

Original comment by fungusak...@gmail.com on 10 Aug 2013 at 10:01

GoogleCodeExporter commented 9 years ago
Here is an example from a popular repository (Celery) of this problem:
https://github.com/celery/celery/blob/fa363ac44df5651caad236f9ccf913e6077d5bfd/c
elery/tests/backends/test_mongodb.py#L179

It goes without anyone noticing because it always passes tests.

Original comment by alan.ham...@gmail.com on 23 Jan 2014 at 3:22

GoogleCodeExporter commented 9 years ago
This bug has catastrophic effects and I believe it is a very wide reaching 
(silent) problem.

Our codebase was affected with this problem. There were hundreds of tests that 
were assumed passing, but the Mock was just absorbing the assert calls as mock 
calls. The root cause being the double duty of the Mock both accepting mock 
calls and assert calls.

The ideal solution being to introduce an expectation class.
e.g. expect(my_mock).toHaveBeenCalledWith()

In its absence I implemented a check in the Mock class to check the attribute 
name before creating a child mock. If the attribute name starts with something 
that looks like an assert call (e.g. 'assert', 'asssert', 'asert', 'assret', 
etc.) then it raises an exception.

I think this is a very important bug to fix and would be happy to help with a 
solution.

Thanks,
Wes

Original comment by he...@wesalvaro.com on 21 Jan 2015 at 1:03