Search code examples
djangomockingcallable

Test Django Mock - check that function/method is called


I want to check that do_a calls do_b. I'm doing like this:

The code:

def do_a(...):
    ...
    do_b(...)
    ...

The test:

def test_do_a(self):
        ...

        with patch('...do_b', new_callable=do_nothing()) as mock_do_b:
            do_a(...)

        mock_do_b.assert_called_once_with(...)

And do_nothing:

def do_nothing():
     pass

This is working fine but I had to use do_nothing() which I find hacky. Is there a way to make the same test without the extra useless function do_nothing()?


Solution

  • You could use patch as a decorator

    @patch('...do_b')
    def test_do_a(self, mock_do_b):
        do_a(...)
        mock_do_b.assert_called_once_with(...)