Reputation: 4061
I have a django view like this
# Django view
from some_module import f2
def f1(request, version):
# some code
f2(**kargs)
# more code
return HTTPResponse(response)
The function f2 is in another module
# some_module
def f2(**kargs):
# some code
The Django view is part of an API so, the request and response are in json
How do I :
EDIT:
The database I am using is Cassandra, so I cannot use django.db
Upvotes: 14
Views: 15839
Reputation: 3133
You can use @mock.patch
decorator to mock f2()
method in your unittests.
import mock
import some_module
from django.test import TestCase
def mocked_f2(**kargs):
return 'Hey'
class YourTestCase(TestCase):
@mock.patch('some_module.f2', side_effect=mocked_f2)
def test_case(self, mocked):
print some_module.f2() # will print: 'Hey'
In this case each time you call f2()
in your code, mocked_f2
will be called.
Upvotes: 26