Created
November 26, 2014 10:15
-
-
Save sigilioso/3b391c1bddb3f1680ce6 to your computer and use it in GitHub Desktop.
Side effect for mocks on object attributes access.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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