Skip to content

Instantly share code, notes, and snippets.

@morenoh149
Last active July 19, 2023 09:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save morenoh149/c9e985c72a0f7d929866dd5c4b3d8a41 to your computer and use it in GitHub Desktop.
Save morenoh149/c9e985c72a0f7d929866dd5c4b3d8a41 to your computer and use it in GitHub Desktop.
Django model method mocking
class Blog(django.model):
name = models.CharField(null=False, max_length=64)
def list_articles(self):
return [
{'body': 'abcdefg'},
{'body': 'abcdefg'},
{'body': 'abcdefg'},
]
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