django - How to mock an object's function which writes to the same object in python? -


class employee:     address = models.foreignkey(address)      def setzipcode(self, zipcode):        setattr(self, 'zipcode', zipcode)      def set_address_property(self, target_property, value):         try:             setattr(self.address, target_property, value)             self.address.save()         except objectdoesnotexist:             # todo. figure out here             print 'cannot save'             return      def get_address_property(self, target_property, default):         try:               return getattr(self.address, target_property)         except objectdoesnotexist:              return default  @property def zipcode(self):     return self.get_address_property('zipcode', none)  @zipcode.setter def zipcode(self, value):     self.set_address_property('zipcode', value)  def functiontotest(employee):     # blah blah blah...     employee.setzipcode(123)  def testfunctiontotest(self):     employee = magicmock()     employee.address.zipcode = none     employee.attribute.setzipcode = lambda x, y: setattr(x.address, 'zipcode', y)     functiontotest(employee)     self.assertequals(employee.address.zipcode, 123) 

if try run test, still employee.address.zipcode none instead of 123. how can fix issue?

i have edited code reflect original logic. help.

you don't need test if attribute set; test should cover if method called @ all:

employee = magicmock(spec= employee) functiontotest(employee) employee.assert_called_once_with(123) 

testing if employee.setzipcode() works correctly subject of unit testing employee class, , outside scope testing fuctiontotest() function.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -