zhilts / mockito-python

Automatically exported from code.google.com/p/mockito-python
0 stars 0 forks source link

Unable to stub and verify __call__ method #5

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
        m = mock()
        when(m).__call__().thenReturn('test')
        print m()

What is the expected output? What do you see instead?
expected: 'test'
Actual: TypeError: 'mock' object is not callable

What version of the product are you using? On what operating system?
any os, with python 2.6.5

Please provide any additional information below.
I also tried with when(m)().thenReturn('test') with no success.

A workaround for this:

        m = mock()
        c = CallableMock( m )               
        when( m ).__call__().thenReturn( 'test1' )        
        c()
        verify( m ).__call__()

class CallableMock(object):
    def __init__(self, mock):
        self.mock = mock

    def __call__(self):
        return self.mock.__call__()

    def __getattr__(self, method_name):
        return self.mock.__getattr__(method_name)

Original issue reported on code.google.com by m.magy...@gmail.com on 3 Mar 2011 at 11:55

GoogleCodeExporter commented 8 years ago
For searching purposes, I had the same problem with iterables, so I wanted to 
stub an __iter__ method. I also did a similar workaround with a subclass of 
mock.

Original comment by ch...@google.com on 30 Dec 2013 at 9:42