0

This is something I discovered and am quite please with but would to understand what is happening. The use case is I want to patch and object in a test using a decorator but I'm not really interested in the mock object.

My old way of doing it.

@patch.object(Foo, 'bar', return_value='fake value')
def test_something(self, unused_foo):

I discovered this works:

@patch.object(Foo, 'bar', Mock(return_value='fake value'))
def test_something(self):

But according to the doc there is no third argument to the patch.object decorator.

patch.object(target, attribute, new=DEFAULT, spec=None, create=False, spec_set=None, autospec=None, new_callable=None, **kwargs)

So what is going on here?

Dan
  • 1,874
  • 1
  • 16
  • 21
  • 3
    There is a third argument. It's `new`, the one after `attribute`. (The fact that it has a default value doesn't mean you have to specify it by keyword.) – user2357112 Oct 28 '16 at 20:06
  • @user2357112 Thanks, but now why is it that I don't need to pass it into my test where as before I do. – Dan Oct 29 '16 at 01:50
  • If you're providing the mock object to the decorator, the decorator doesn't need to provide it to you. The docs mention that this happens. – user2357112 Oct 29 '16 at 01:57
  • What I usually do when I have patches that I don't need their mock object is add a last argument in the test signature of `*args`. Something like `def test_x(self, *args):` – Uri Shalit Nov 09 '16 at 12:52

0 Answers0