derwiki-adroll / mock

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

Patch decorator to gather params into "p" object arg #138

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi!

After having tons of this type of code:

@patch('baz_function')
@patch('bar_function')
@patch('foo_function')
def test_should_do_something_useful(
self, foo_function_mock, bar_function_mock, baz_function_mock):

    foo_function_mock.return_value = 123
    ...

I was really pissed off of number of copypaste-job you need to do every time 
you want to patch something. So I suggest us to have something like this:

@patch('baz_function')
@patch('bar_function')
@patch('foo_function')
def test_should_do_something_useful(self, p):
    p.foo_function.return_value = 123
    ...

Much better now. Of course, I will implement it in my own code (as external 
hack under @patch), but I think it's cool to have in mock itself too.

Thanks!

Original issue reported on code.google.com by k...@k-bx.com on 3 Mar 2012 at 1:55

GoogleCodeExporter commented 9 years ago
Well, it would be an incompatible change to patch - so if it *was* to be built 
into mock it would have to have a different name. It's a neat idea but I don't 
think it needs to be in mock itself. If you post it as a recipe on a blog (or 
here) I'll happily put it in the advanced example page of the docs though.

Consider this problem though, patching is always done as a *dotted name*. e.g.

@patch('os.path.split')
def test(p):
   ...

Should this be p.os.path.split or p.split ? If the former then you have to 
construct the useless intermediate objects. If the latter, what do you do here:

@patch('os.path.split')
@patch('re.split')
def test(p):
   ...

Which mock does "p.split" point to?

Original comment by fuzzyman on 4 Mar 2012 at 12:00