I have a function foo which is something like this:
class SomeClass(object):
def foo(self, url):
try:
r = requests.get(url)
buffer = StringIO.StringIO(r.content)
except Exception as e:
pass
I am trying to test it with Python mock library by doing something like this:
class FooTest(unittest.TestCase):
def test_foo(self):
obj = SomeClass()
with patch('requests.get', MagicMock()):
with patch('StringIO.StringIO', some_fake_method):
obj.foo()
However, doing it this way would not patch any of them and I would instead get proper response
and StringIO
objects. If I omit StringIO
from the patch, I get a MagicMock
object as expected (instead of a response
object). How do I make this work properly?