spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

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 the 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

class MyManager(object):

    def dowork(self, value1, value2):
        print value1, value2

class MyModel(object):
    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
        pass

    def call_manager(self):
        self.objects.dowork(self.value1,
self.value2)

def new_dowork(self, value1, value2):
    print "Mock called"
    print 123

with patch.object(MyManager, 'dowork',
new=new_dowork):
    record = MyModel(value=111)
    record.call_manager()

This outputs:

Mock called
123

Categories : Python

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]): self._do_stuff() The name of the list is 'PARTNERS'. This seems blindingly obvious now. Thanks for the patience in my learning process. Hopes this helps someone else.

Categories : Python
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

Categories : Python
Kivy/Python - calling function with 3 arguments without giving any
UrlRequest(search_url, self.found_location) 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: self.found_location(sth,sth) No. The point is that you don't want to call the function, only to p

Categories : Function
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): return constant+a+b 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): ...

Categories : Python
Python mock patch argument `new` vs `new_callable`
[EDIT] 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 notes. 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

Categories : Python
Recently Add
Adding json to new Django database
document clustering in python
Only read the last character in a .txt file
Is it possible to redirect to different domain retaining the trailing endpoint, from a route?
Python NameError: not defined
Numpy array loop
Turning off Tick Marks in Bokeh
Python: Number and operator concatenation not working (Euler's Method)
Python: Twitter API tweets/search: Flatten nested dictionary to columns
Python 2.7 cmd autocomplete readline buffers seem stale
Memory usage/efficiency for pandas dataframe versus lists versus tuples, etc.
How can i use multiple lists as arguments in a function and receive them differently?
PyQt - setText method of QTableWidget gets AttributeError
Matplotlib - get value of autoscale
Backwards axes in numpy.delete
Twilio - How to determine the length of a conference call?
Counting string using for loop
Automating creation of class instances in python for an undetermined amount of instances
Printing 2D-array in a grid
Load PreComputed Vectors Gensim
IPython _repr_html_
Reversing a number using recursion
Is there a configuration under which a numpy operation will work on more than a single core/thread?
Is there a better way to write this if-statement?
Python: Effective reading from a file using csv module
Django/Python: CSV for-in loop overriding first row each time through
How to read the pickled igraph graph object from old version by new version igraph
Tornado WebSocket with Django ORM with shared session
trying to plot contours of bivariate normal, won't work with a correlation term
Python split users input
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.