I have the following script:
in my models.py
class FooBar(models.Model): description = models.CharField(max_length=20)
in my utils.py file.
from models import FooBar def save_foobar(value): '''acts like a helper method that does a bunch of stuff, but creates a FooBar object and saves it''' f = FooBar(description=value) f.save()
in tests.py
from utils import save_foobar @patch('utils.FooBar') def test_save_foobar(self, mock_foobar_class): save_mock = Mock(return_value=None) mock_foobar_class.save = save_mock save_foobar('some value')
This is a simplified version of what I'm trying to do ... so please don't get stuck on why I have a utils file and a helper function for this (in real life, it does a few things). Also, note that while this is simplified, this is an actual working example of my problem. The first call to check call_count returns 1 and passes. However, the second returns 0. Thus, it seems to me that my patch is working and receiving a call.
How can I verify that not only an instance of FooBar is being created, but also that the save method on it is called?
source share