Question
I'm trying to mock something while testing a Django app using the imaginatively named Mock testing library. I can't seem to quite get it to work, I'm trying to do this:
- models.py
- #!/usr/bin/env python3
- class Promotion(object):
- def __init__(self):
- pass
- def bar(self):
- return "Do something I don't want!"
How-To
Check below sample code:
- test.py
- import models
- from unittest import TestCase
- from unittest.mock import patch
- class ViewsDoSomething(TestCase):
- def setUp(self):
- self.mockMsg = 'Do what I want'
- @patch.object(models.Promotion, 'bar')
- def test_enter_promotion(self, mockObj):
- mockObj.return_value = self.mockMsg
- p = models.Promotion()
- self.assertTrue(p.bar() == self.mockMsg)
Supplement
* Python 文章收集 - 用 Mock 來做 Python Unit Test
* Python 文章收集 - pytest introduction
沒有留言:
張貼留言