Skip to content

Instantly share code, notes, and snippets.

@morenoh149
Last active Jun 21, 2021
Embed
What would you like to do?
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