I'm writing an API in Python and I'm using Flask-RESTFul. I have a Login method which receives Email and Password as request parameters, both are required:
class Login(Resource):
def __init__(self):
self.parser = reqparse.RequestParser()
self.parser.add_argument('email', required=True)
self.parser.add_argument('password', required=True)
The code works just fine. If I try a request via Postman without request parameters, Flask-RESTFul sends a response error that Email and Password are required fields.
Now I want to write a unit test for this. The test should actually be pretty simple:
@mock.patch('flask_restful.reqparse.RequestParser.parse_args')
class TestLoginMethods(unittest.TestCase):
def test_post_body_missing(self, parse_args):
self.login = Login()
self.login.post()
# TODO: should assert error when Email and Password not found in request
The problem here is, that parse_args gets mocked and the functionality of Flask-RESTFul of raising error for the required fields is gone. That means, no error is returned if Email and Password have not been provided. How do I stub this functionality of RequestParser? I'm new in python, I could find only examples without Flask-RESTFul.