Created
December 16, 2015 16:46
-
-
Save chfw/ce131c95da283ede3256 to your computer and use it in GitHub Desktop.
How to mock django model's delete method
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
def do_it(token, logger): | |
try: | |
tm = AMODEL.objects.get(unique_id=token) | |
tm.delete() | |
except Exception as e: | |
logger.error("...") | |
class FakeLogger: | |
def __init__(self): | |
self.message = "" | |
def error(self, message): | |
self.message = "error:" + message | |
class ModelDeleteMethodTest(TransactionTestCase): | |
@patch('app.models.AMODEL.delete') | |
def test_exceptional_error(self, FakeMethod): | |
reason = "test" | |
FakeMethod.side_effect = Exception() | |
logger = FakeLogger() | |
do_it(self.token_id) | |
assert logger.message == "error:..." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment