derwiki-adroll / mock

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

Make nesting patch decorators more clear #231

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
http://www.voidspace.org.uk/python/mock/patch.html#nesting-patch-decorators

It might be just me not being attentive enough, but it took me quite a while of 
debugging before I found out that the order of arguments within the test method 
is reversed from the order of patches.

Is there any particular logic/reason behind that?

If I take the example from documentation and slightly change the naming to make 
it more obvious:

@patch.object(SomeClass, 'first_method') @patch.object(SomeClass, 'second_method') def test(second_method, first_method):


I think people in most countries over the world read from left to right and 
from top to the bottom.
I don't expect anybody changing the API as that change would confuse many 
people already using the old API, but it should be imo much more obvious from 
the documentation - at least from the names of arguments.

I believe that even such a small change as `test(mock1, mock2)` to `test(mock2, 
mock1)` would make a difference.

Original issue reported on code.google.com by radek.simko on 30 Apr 2014 at 10:32

GoogleCodeExporter commented 9 years ago
See the note in the documentation:  

Note that the decorators are applied from the bottom upwards. This is the 
standard way that Python applies decorators. The order of the created mocks 
passed into your test function matches this order.

Your "intuition" that decorators should be read top down is actually wrong I'm 
afraid, and it would be odd if patch worked differently from "normal" 
decorators. The arguments are passed in in the order that decorators are 
applied.

Original comment by fuzzyman on 30 Apr 2014 at 5:07

GoogleCodeExporter commented 9 years ago
That's understandable but what I was trying to say is that this small change 
wouldn't hurt anyone I think:

change `test(mock1, mock2)` to `test(mock2, mock1)`

it would just make it more obvious.

Original comment by radek.simko on 30 Apr 2014 at 5:13