lundi 3 décembre 2018

How to mock function call in flask-restul resource method

I developed an API using Flask-restful. I have an API with resource named 'Server'. This resource has a method get to process request to '/server' url. In this method I have a call method of another class 'Connector' that get data from another service:

 class Server(Resource):
    def get(self):
     ...
        status, body = connector.get_servers(page, size) # call method of another class
     ...
    return body, status

I want to test developed API. I wrote some tests:

from application import create_app
from unittest import TestCase

class TestServerResource(TestCase):

def setUp(self):
    self.app = create_app()
    self.client = self.app.test_client

def test_bad_url(self):
    res = self.client().get('/server')
    self.assertEqual(res.status_code, 400)

# Test of get method Server resources described above
def test_pagination(self):
    res = self.client().get('/server?page=1&size=1') # request to my API
    self.assertEqual(res.status_code, 200)

In method 'test_pagination' I am testing method 'get' of my resource, but call of method of another class is in this method. Therefore I have a question: how I can mock call of 'connector.get_servers()' in test?

Aucun commentaire:

Enregistrer un commentaire