Python mocking: missing 1 required positional argument

Viewed 8496

I'm writing tests for my Flask project, and are trying to mock my database model. The code looks something like this:

import unittest.mock

@unittest.mock.patch("server.models.user")
def test_that_response_contain_correct_user_data(self, mocked_user):

This results in this error message:

TypeError: test_that_response_contain_correct_user_data() missing 1 required positional argument: 'mocked_user'

So it looks like the mocking framwork doesn't inject the mocked data into the function. Does anyone know what may be causing this?

1 Answers

You need to import patch from unittest.mock and use that decorator to inject the mocked data.

from unittest.mock import patch

@patch("server.models.user")
def test_that_response_contain_correct_user_data(self, mocked_user):
Related