vendredi 25 mars 2016

Sinon.js - How To Stub a Function You do not have Access To

I am trying to write some unit tests for a .js file. The function I am writing tests for currently has the following structure:

var myfile = function(sid, token){
  client = require('anotherFile')(param1, param2); 
};

myfile.prototype.function1 = function(query, dbClient, callback){
  if(!callback) return;
  if(!query) return callback("No query data passed to function1.");
  if(!query.userNum) return callback("No number to send to passed to function1.");
  if(!dbClient) return callback("No dbClient passed function1.");

  client.clientsMethod(anObject, function(err, message) { 
    if(err){
      return callback(err);
    } else {
      dbClient.clientMethod(param1, function(err){
        if(err){
          return callback(err);
        }
        return callback(true);
      });
    }
  });
}

module.exports = myFile;

The issue is that I need to create a myFile object to create the method but I need to also have access to client.clientsMethod to be able to spy on it. I have tried calling this in test file:

client = require('anotherFile')(param1, param2); 

but stubbing client and trying to use:

 myfile.function1
 assert.ok(client.called)

doesn't work because it is referencing the object I created in test file and not the one in the myfile file.

Aucun commentaire:

Enregistrer un commentaire