Reputation: 6330
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 what to do 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 I try to run this test, I still get employee.address.zipCode
as None
instead of 123. How can I fix this issue?
Upvotes: 0
Views: 162
Reputation: 1123450
You don't need to test if the attribute is actually set; your test should only cover if the method is called at all:
employee = MagicMock(spec= Employee)
functionToTest(employee)
employee.assert_called_once_with(123)
Testing if employee.setZipCode()
works correctly is the subject of unit testing the Employee
class, and is outside the scope for testing the fuctionToTest()
function.
Upvotes: 4