you could do 2 things here, the simplest its just patch Pyro4.Proxy, something like this:
from unittest.mock import patch
# or from mock import create_autospec if you are in py2
class TestA(TestCase):
@patch('Pyro4.Proxy', autospec=True)
def test_method(self):
temp_A = A()
temp_A.method_1()
the trick is to patch the "right object" and don't instantiate the class till after you patch (inside your test function) , Pyro4.Proxy
is the best object i could deduce from your code, but if your class A
is in my_module, maybe the best object to patch is my_module.Pyro4.Proxy
.
the other thing you could want is to just mock the var variable. then as you define self.var
inside the __init__
method and assuming you don't want to mock the whole __init__
method then you can
from unittest.mock import create_autospec
# or from mock import create_autospec if you are in py2
class TestA(TestCase):
def setUp(self):
self.temp_A = classA()
self.temp_A.var = create_autospec(Pyro4.Proxy)(None)
def test_method(self):
self.temp_A.method_1()
if you can refactor your class a bit i would suggest make the var a property for easy mocking
class A(object):
def __init__(self):
...
@property
def var(self):
return Pyro4.Proxy(self.ns.lookup(temp_name))
def method_1(self):
.....
and then your testing class can be
from unittest.mock import create_autospec, PropertyMock
class TestA(TestCase):
def setUp(self):
self.temp_A = classA()
type(self.temp_A).var = PropertyMock(side_effect=lambda *x, **y: create_autospec(Pyro4.Proxy)(*x, **y))
def test_method(self):
self.temp_A.method_1()