The Mir
The Mir

Reputation: 450

How to Mock a function in Python(Flask)

I don't have any idea what should I mock.

I have a function that takes a 'username' argument and deletes a user from list of users based on the passed 'username' :

users = [
    {"username": "John", "email": "[email protected]"},
    {"username": "Smith", "email": "[email protected]"},
]


@app.route("/students/<string:username>", methods=
["DELETE"])
def delete_student(username):
    user = list(filter(lambda x: x["username"] == username, users))
    if user:
        users.remove(user[0])
        return "", 204
    else:
        return "", 404

How do I should mock this function? What do I should mock?

Upvotes: 0

Views: 77

Answers (1)

J&#252;rgen Gmach
J&#252;rgen Gmach

Reputation: 6093

There is no need to patch the function, but the module level dictionary.

from unittest.mock import patch

with patch.dict('yourmodule.users', {'newkey': 'newvalue'}):
    ... # here goes the test call

Upvotes: 1

Related Questions