Using coverage to look what has to be tested, and coverage shows that next to this has to be tested:
send_alert.apply_async()
I know it is celery task, but is there any way to test the line of code?
rest of code to understand logic for test:
class SomeView(GenericAPIView)
def post(self, request, *args, **kwargs):
#some code
if not value:
send_alert.apply_async()
# response
# not if Response
Test written due to first answer
@patch('event.views.send_alert')
def test_send_alert(self, mock_send):
data = {some data for post}
response = self.client.post('/api/{0}/mettings/'.format(self.user), data)
print(response.json())
self.assertTrue(mock_send.called)
Also i check in view that after task print('Tasks Passed')
and i see message that tasks passed, but test failed with error AssertionError: False is not true
I'm assuming you are using Django, as this is tagged that way.
Decorate your test with:
@override_settings(CELERY_EAGER=True)
def test_foo(self):
# Make your post
Or if you just want to test that it is called.
from mock import patch
@patch('your_module.your_file.send_alert.apply_async')
def test_foo(self, mock_send):
# make your post with "value" set to True
self.assertTrue(mock_send.called)