mercredi 21 janvier 2015

How do I mock a class in a Python unit test?

I have a class:



class A:
__init__(self):
self.b = B()

def is_authorized(self)
name = self.b.get_name()

if name == 'admin':
return True
else:
return False


I'd like to write a unit test to test the is_authorized method. The problem is that it requires an instance of the B class which is very complicated to construct in isolation as it requires network connections and other stuff. How can I mock this class and provide something that just has the get_name method. So that I can create the A class and test the method.


Aucun commentaire:

Enregistrer un commentaire