Last active
July 19, 2023 09:18
-
-
Save morenoh149/c9e985c72a0f7d929866dd5c4b3d8a41 to your computer and use it in GitHub Desktop.
Django model method mocking
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
class Blog(django.model): | |
name = models.CharField(null=False, max_length=64) | |
def list_articles(self): | |
return [ | |
{'body': 'abcdefg'}, | |
{'body': 'abcdefg'}, | |
{'body': 'abcdefg'}, | |
] |
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
import mock # python 2.7 | |
@mock.patch('blog.models.Blog.list_articles') | |
class BlogFunctionalTestCase(django.test.TestCase): | |
def test_blog__list_articles(self, mock_list): | |
blog = Blog.objects.create( | |
name = 'abc blog' | |
) | |
mock_list.return_value = [ | |
{'body': 'abcdefg'}, | |
{'body': 'abcdefg'}, | |
{'body': 'abcdefg'}, | |
] | |
articles = blog.list_articles() | |
self.assertIsNone(articles) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment