Python mock patch a function missing arguments
I don't know much about Mock, but I think the main
issue is that you should pass the mock function in
new argument, not in
new_callable, and use patch.object to actually
overwrite a method within an object.
This seems to work:
from mock import patch
def dowork(self, value1, value2):
print value1, value2
objects = MyManager()
value1 = 'foo'
value2 = 'bar'
def __init__(self, value=0):
# You don't need this __init__... Is just
so I don't have to
# use models.MyModel thingy
def new_dowork(self, value1, value2):
print "Mock called"
with patch.object(MyManager, 'dowork',
record = MyModel(value=111)
|Related to : Python mock patch a function missing arguments|
|In python is there a way to patch a list similiar to using patch.dict?|
Ok I feel kinda dumb but just figured out how to do it.
with patch.object(integrations, 'PARTNERS',[partner_type]):
The name of the list is 'PARTNERS'. This seems blindingly obvious now.
Thanks for the patience in my learning process. Hopes this helps
|How to promulgate a list of arguments for a python function?|
I'm going ahead with a variant on the approach I outlined in my
original post, in which I attach an attribute to __init__. However,
instead of making it a list, I am taking the more flexible approach of
using a function. I define an unattached function argNames that takes
one or more classes or functions as arguments and returns a set of
argument names, using inspect and, recursively, calls to the
|Kivy/Python - calling function with 3 arguments without giving any|
This passes UrlRequest the two parameters, the second of which is the
function itself. UrlRequest automatically calls that function with two
arguments, specifically itself and the data it has retrieved.
Shoudn't the call in UrlRequest look more like:
No. The point is that you don't want to call the function, only to p
|Function with fewer arguments and optional arguments|
You can use functools.partial to make this a lot easier:
from functools import partial
def myfunc_general(constant, a=1, b=2):
myfunct_specific1 = partial(myfunc_general, a=10)
myfunct_specific2 = partial(myfunc_general, a=20)
Below is a demonstration:
>>> from functools import partial
>>> def myfunc_general(constant, a=1, b=2):
|Python mock patch argument `new` vs `new_callable`|
In that answer I missed the most important thing that new take an
object and new_callable a callable (the factory) as said correctly by
@chepner. I don't remove it because it contains either some useful
By digging the code
https://code.google.com/p/mock/source/browse/mock.py#1266 is clear
that by new you cannot set the attributes by nominal arguments and use
spec or spec_set because