python - Error when mocking modules with the patch decorator in Django -
I'm learning about joke in Dango but my head can not be found around patching.
Test_get_name passes but test_patched_get_name Here are my files: utils.py tests.py models.py I think Full Traceback: If you have the Get_name function, I think that you mean instead should Django's get_object_or_404 patch that when Test call get_name , then Dijengo the get_object_or_404 instead of calling Code will call a fake item - Patch C function - which will return some Purwanumanyukt value in stuff to test is simply to verify that get_name to work and return the correct value. It does not need to make a real call to Dezango's get_object_or_404 (since this is not what you are testing). I think your test should look something like this: fails with value error: Object is of 'MagicMock' type, but a Django model, manager, or QuerySet
def get_name (clientid): c = get_object_or_404 (client, pk = clientid ) C_name = c .get_name () return c_name
class client entity ests (unittest.TestCase): def Set up (self): self.c_instance = mock.mock (idea = client, p = 1) self.c_instance.name = "test" def test_get_name (self): self.assertEqual (Clie nt.get_name (self.c_instance), "Test") def test_patched_get_name (self): mock.patch ( 'myapp.utils.Client') as well as self.c_instance: myapp.utils import get_name self.assertEqual ( Get_name (self.c_instance.pk), "test")
class client (model.model ): Name = model.carfield (max_long = 200) def get_name (self): Return self.name
Get_object_or_404 a model instead of a duplicate object But I'm not sure how to do it.
===== ========================= ======================================== Error: test_patched_get_name (myapp.tests.ClientTests) ------------------------- ------------------------- -------------------- Tracebacks (last most recent call): File "pathto / MyApp / tests.py", line 63, test_patched_get_name self.assertEqual (get_name (self .c_instance.pk) file "pathto / MyApp / views.py", line 36, in get_name cu = get_object_or_404 (client, P = clientid) file "pathto / lib / python2.7 / site-packages / Django / shortcuts / __ init__.py ", line 111, get_object_or_404 queryset = _get_queryset (Klass) file" pathto / lib / python2.7 /site-packages/django/shortcuts/__init__.py ", line 97, _get_queryset in "Manager, or QuerySet group" in% klass__name) ValueError: Object type of 'MagicMock', but should be a Django model, manager, or query group - ------------------ -------------------------------- ------------ ------ -
# Make sure that you get the "get_name" inside the "patch" import patch from the fake import patch # working @patch ( 'myapp.utils.get_object_or_404') has def test_patched_get_name (used manually, mock_get_object_or_404 "get_object_or_404"): # patch function is provided as a parameter to your test function # - you call it by any name (In this case, it is # "mock_get_object_or_404") Import Myapp.utils get_name # Set the return value of counterfeit function - to look at patching function # fake "client" object will return Mock_get_object_or_404.return_value = self.c_instance # longer than the return value of "get_name" function # what This is similar to the name of the dummy client. Return_value = get_name (self.c_instance.pk) expected_value = self.c_instance.name self.assertEqual (returned_view, expected_value)
Comments
Post a Comment