I am writing unit tests for a Node.js application, and I am wondering if I am mocking the correct parts of the code.
The example below is a hypothetical class that has two static methods.
The method "isTokenValid" calls another method, "decodeToken," which takes the token and a callback. The callback is defined inside of "isTokenValid." Both these methods belong to the same class.
When unit testing "isTokenValid," my approach is to mock the "decodeToken" method.
It is clear to me that when unit testing, dependencies such AJAX requests should be mocked. However, does that also hold true for this type of dependency or am I being too granular? Is mocking "decodeToken" the right approach to unit testing "isTokenValid"?
`var TokenClass = {};`
`TokenClass.isTokenValid(token) {
TokenClass.decodeToken(token, function(err, decoded) {
if(err) {
console.log('There was a validation error');
}
if(decoded){
return true
};
}
}`
`TokenClass.decodeToken(token, callback) {
// some logic here to decode token
if(err) {
return callback(err);
}
// if token is not valid
if(!validToken) {
return callback(null, undefined);
}
// if token is valid
return callback(null, decoded);
}`
Aucun commentaire:
Enregistrer un commentaire