3

I have the following code, which I need to test:

uploader = AjaxFileUploader(backend=XLSXFileUploadBackend, obj=obj)
response = uploader(request)
...
response.content

I'd like to mock AjaxFileUploader and replace the response.content with some value.

I do the following:

@patch('guinness.apps.home.views.AjaxFileUploader')
def test_import_asset_to_bucket(self, mock_file_uploader):
    test_content = 'a:"1"'
    mock_file_uploader.return_value = MagicMock(return_value=MagicMock(content=test_content))

Is there more elegant way to do it? Thanks!

1 Answer 1

2

You should be able to get away with it just doing

mock_file_uploader.return_value.return_value.content = test_content

because the attribute return_value returns a new Mock object which is created on first access, as described in the documentation.

I'm not sure this is much more elegant, though.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.