jeudi 2 juillet 2015

How to make sure that a function doesn't get called using Mock in a Django Application

I've already browsed through the mock documentations, and i've seen some examples where mock has been used. But, being a newbie, i find it hard to use mock in my tests.

test_scoring.py - I am creating a test to make sure that a function DOES NOT get called whenever i create an item.

The function compute_score() that i'd like to mock is part of a class HistoryItem.

What i got so far is this :

#test_scoring.py

@mock.patch('monitor.report.history_item.HistoryItem.compute_score')
def test_save_device_report(self):
    ....
    result = factory.create_history_item(jsn)
    # If the mocked method gets called after the above function is used, then there should be an error. 

So, how do I mock the method? I'm quite confused on how to use it, because there are different ways in the resources i found.

I'd really appreciate your help.

Aucun commentaire:

Enregistrer un commentaire