Skip to content

Instantly share code, notes, and snippets.

@sigilioso
Created November 26, 2014 10:15
Show Gist options
  • Save sigilioso/3b391c1bddb3f1680ce6 to your computer and use it in GitHub Desktop.
Save sigilioso/3b391c1bddb3f1680ce6 to your computer and use it in GitHub Desktop.
Side effect for mocks on object attributes access.
from unittest import TestCase
import mock
from django import models
from django.core.exceptions import ObjectDoesNotExist
class Foo(models.Model):
# ...
@property
def has_pending_related(self):
try:
return self.related_field.is_pending
except ObjectDoesNotExist:
return False
class FooTestCase(TestCase):
# ...
@mock.patch(Foo, 'related_field', new_callable=mock.PropertyMock)
def test_pending_related(self, related_field):
related_field.side_effect = ObjectDoesNotExist
foo = Foo()
self.assertFalse(foo.has_pending_related)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment