pythondjangomockingpython-mock

Django Mock isn't working as expected in Unit test


I'm having a view in which there is a method called can_upload_file that makes external calls. This method is called from view's post (create) method. So, in my unit test I'm trying to test the post method and at the same time mock can_upload_file method, as follows:

DatasetViewSet._can_upload_file = MagicMock()
DatasetViewSet._can_upload_file = "valid"

response = self.client.post(self.url, self.dataset, format="multipart")

However I see that the actual can_upload_file method in the view is still executed.

How can I fix this?


Solution

  • You can specify the return value of a mocked method like this:

    DatasetViewSet._can_upload_file = MagicMock(return_value="valid")