mercredi 19 juin 2019

What is the point of mocking the method of an external library when unit testing?

What is the point of mocking the method of an external library when unit testing?

Let's say that I have a function that uses XYZ library to get the current user using a given authorization token, and then if it finds the user, it returns a valid AWS policy:

export const getUser = async token => {
  const user = await XYZ.getUser(token)
  return {
    // valid policy
    context: user,
  }
}

If an invalid token is given, the getUser should throw an error.

  • What is the point of testing this function if the XYZ.getUser is already well tested?
  • Why mock the getUser instead of using the real one?

Aucun commentaire:

Enregistrer un commentaire