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?
blhsing
109k9 gold badges89 silver badges132 bronze badges
1 Answer 1
You can specify the return value of a mocked method like this:
DatasetViewSet._can_upload_file = MagicMock(return_value="valid")
answered May 3, 2023 at 2:32
blhsing
109k9 gold badges89 silver badges132 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Neo
thanks a ton @blhsing I was struggling with this for hours together and was really losing it. Much thanks again..!!
lang-py