[Answered ]-Mocking a request object to pass to ViewSet create() method

1👍

What I ended up doing to resolve the issue was the following:

# test.py

import unittest
from unittest.mock import patch

from core.views import KeysViewSet


class KeysViewSetTestCase(unittest.TestCase):

    def setUp(self):
        self.json_object = {'key': 'hello', 'value': 'world'}
        self.view = KeysViewSet()
        class Request:
            def __init__(self, data):
                self.data = data
        self.request = Request(self.json_object)


    def test_create(self):
        with patch('core.views.RedisUtil.create') as mocked_create:
            mocked_create.return_value.data = True

            created = self.view.create(self.request)

That being said, I’m not sure that this is a desirable solution so I’m reluctant to accept it as the correct answer. Looking forward to feedback.

👤cjones

Leave a comment