hRobert12 / mock

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

Can't create_autospec with name argument #224

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

Run the following code:

import mock
def a():
 print "blah"
q = mock.create_autospec(a, name="a")

What is the expected output? What do you see instead?

I expected the creation of an autospec'd mock with the name "a". Instead, the 
following exception is raised:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/mock.py", line 2186, in create_autospec
    name=_name, **_kwargs)
TypeError: type object got multiple values for keyword argument 'name'

What version of the product are you using? On what operating system?

Mock 1.0.1 on arch linux.

Please provide any additional information below.

Creating a mock with a name attribute works fine.

Original issue reported on code.google.com by plasmash...@gmail.com on 8 Apr 2014 at 12:14

GoogleCodeExporter commented 8 years ago
The correct keyword is "_name" instead of "name". The latter would be more 
obvious though.

>>> class A(object):
...   pass
... 
>>> import mock
>>> a = mock.create_autospec(A, _name='A')
>>> a
<MagicMock name='A' spec='A' id='140235987196624'>

Original comment by sieb...@gmail.com on 18 Dec 2014 at 12:13

GoogleCodeExporter commented 8 years ago
How is anyone supposed to know this? The documentation for create_autospec says:

>create_autospec also takes arbitrary keyword arguments that are passed to the 
constructor of the created mock.

The documentation for mock says:

>name: If the mock has a name then it will be used in the repr of the mock. 
This can be useful for debugging. The name is propagated to child mocks.

Absolutely bizarre.

Original comment by plasmash...@gmail.com on 18 Dec 2014 at 6:53