Skip to content

Instantly share code, notes, and snippets.

@chfw
Created December 16, 2015 16:46
Show Gist options
  • Save chfw/ce131c95da283ede3256 to your computer and use it in GitHub Desktop.
Save chfw/ce131c95da283ede3256 to your computer and use it in GitHub Desktop.
How to mock django model's delete method
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