0

I have the following code that I'm attempting to create a test (still work in progress):

from core.tests import BaseTestCase                                                                          
from core.views import get_request                                                                           

from entidades.forms import InstituicaoForm                                                                  
from mock import patch                                                                                       


class InstituicaoFormTestCase(BaseTestCase):                                                                 

    def setUp(self):                                                                                         
        super(InstituicaoFormTestCase, self).setUp()                                                         

    @patch('get_request', return_value={'user': 'usuario_qualquer'})                                         
    def test_salva_instituicao_quando_informaram_convenio():                                                    
        import pdb                                                                                              
        pdb.set_trace()                                                                                         
        form = InstituicaoForm()

it fails because when I try to create a InstituicaoForm, a get_request is called:

def get_request():
    return getattr(THREAD_LOCAL, 'request', None)

and it trows this error

entidades/tests.py:11: in <module>
    class InstituicaoFormTestCase(BaseTestCase):
entidades/tests.py:16: in InstituicaoFormTestCase
    @patch('get_request', return_value={'user': 'usuario_qualquer'})
.tox/unit/local/lib/python2.7/site-packages/mock/mock.py:1670: in patch
    getter, attribute = _get_target(target)
.tox/unit/local/lib/python2.7/site-packages/mock/mock.py:1522: in _get_target
    (target,))
E   TypeError: Need a valid target to patch. You supplied: 'get_request'
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> entering PDB >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> /home/vinicius/telessaude/.tox/unit/local/lib/python2.7/site-packages/mock/mock.py(1522)_get_target()
-> (target,))

What am I doing wrong? How should mock this get_request() method?

1
  • 1
    See the mock docs on where to patch. Note that using threadlocals is generally considered a bad idea, and there is rarely a need to do so in Django code. Commented Aug 4, 2017 at 20:54

1 Answer 1

2

I think the specific thing you're trying to do can be done like this:

@patch('core.views.get_request', return_value={'user': 'usuario_qualquer'})

But you should also look at the Django testing documentation, if you haven't already. You can use the testing client to fake the web request.

If you want to experiment with mock tests that don't access a database, check out Django Mock Queries. (I'm a small contributor to that project.) I've also tried mocking views, but it's fiddly.

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.