Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for mock calls with wildcards?

I am writing unit tests, and want to check for a call which has a function object, like so:

call(u'mock', u'foobar', <function <lambda> at 0x1ea99b0>, 10)

How do I check that the call() has all the parameters I want, without reproducing the lambda?

Edit: I wanted to clarify that I am using the mock library from here: http://mock.readthedocs.org/en/latest/. The call I showed above is a call on a MagicMock object, which I want to check using assert_has_calls.

like image 955
Rohan Dhruva Avatar asked Mar 04 '14 22:03

Rohan Dhruva


1 Answers

I finally found out how to do what I want. Basically, when using assert_has_calls, I wanted one parameter to match regardless of what it was (because I can't recreate the lambda every time during test).

The way to do it is to use mock.ANY.

So, in my example, this could match the call:

mocked_object.assert_has_calls([    call('mock', 'foobar', mock.ANY, 10) ]) 
like image 177
Rohan Dhruva Avatar answered Oct 04 '22 03:10

Rohan Dhruva