1

I have a view function similar to

def my_function(request):
    session = create_something('some_random_string')
    return redirect(session.url, code=303)

To test it

import unittest
from django.test import TestCase
from unittest.mock import patch
from my_app.views import my_function

class TestMyFunction(TestCase):

    @patch('my_app.views.create_something', return_value={
        "url": "https://tiagoperes.eu/"
    })
    def test_my_function(self, mock_create_something):
        response = self.client.get("/my-function/")

This gives

AttributeError: 'dict' object has no attribute 'url'

This question is similar the following questions

1 Answer 1

0

I had to substitute the return_value in patch to use MagicMock

@patch('my_app.views.create_something', return_value=MagicMock(url="https://tiagoperes.eu"))

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.