1

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
asked May 3, 2023 at 2:27

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a ton @blhsing I was struggling with this for hours together and was really losing it. Much thanks again..!!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.