I have a method that I'm trying to test and I would like to mock the methods that my method under test calls. This mocked method calls a method which calls another method and so on. I would like to mock the return value of the top level method, but the return value is lost. I suspect that I am not patching in the right place, but I can't figure out where I should patch. I've read: Where to patch, but I can't tell where I'm going wrong.
The method I'm trying to test is get_url
, the first thing it does is create an instance of Bar
class. Bar
then calls the method I want to mock, get_obj
. result
should consist of a list of objectLocation
objects, each one of which will have a url. However the only thing I get back is a mock object with no return value.
src.delta.FooClass.py
import BarClass
class Foo(object):
def get_bar(self):
bar = BarClass.Bar()
return bar
def get_url(self):
bar = self.get_bar()
result = bar.get_obj()
res = []
for location in result.objectLocation:
res.append(location.url)
return res
src.delta.BarClass.py
class Bar(object):
def get_obj(self):
return BarObj()
class BarObj(object):
def __init__(self):
self.objectLocation = []
for idx in range(0,2):
loc = BarLocation()
self.objectLocation.append(loc)
class BarLocation(object):
def __init__(self):
self.url = 'www.example.com'
src.test.test_foobar.py
import unittest
import src.delta.FooClass as foo
from mock import patch
class Test(unittest.TestCase):
def setUp(self):
self.client = foo.Foo()
@patch('src.delta.FooClass.BarClass')
def testName(self,mocked_d1):
mocked_d1.objectLocation.return_value = ['www.example.com']
output = self.client.get_url()
self.assertEqual(['www.example.com','www.example.com'],output)
if __name__ == "__main__":
unittest.main()
Aucun commentaire:
Enregistrer un commentaire