mercredi 23 décembre 2015

How to change the class behavior using mock? [on hold]

For example, I have the following logic in my Django test:

from django.test import TestCase
from django.core.urlresolvers import reverse

class MyViewTest(TestCase):

    def test_some_view(self):
        response = self.client.get(reverse('my-url-name'))
        self.assertEqual(response.status_code, 200)

and I want to change the logic of SomeProcessing method (which works in a view for 'my-url-name' url-pattern) in a way that it just does nothing. Neither returns nor processes something, just nothing (e.g. sending SMS messages while testing etc.), like it only has a "pass" in the body.

I tried to use this notation:

from mock import patch 
def test_some_view(self):
    with patch('full.path.to.the.class.and.its.method') as perm_mock:
        perm_mock.return_value = None
        response = self.client.get(reverse('my-url-name'))
        self.assertEqual(response.status_code, 200)

but, unfortunately, it doesn't work this way, and I have no idea why.

How to avoid the behavior of this method using entire mock or it's @patch decorator?

Aucun commentaire:

Enregistrer un commentaire