Ho dei problemi di scrittura di unit test per la seguente configurazione come file jira.js (in un modulo node.js):nodo js Unit Testing: beffardo richiede dipendenza
var rest = require('restler'); // https://www.npmjs.com/package/restler
module.exports = function (conf) {
var exported = {};
exported.getIssue = function (issueId, done) {
...
rest.get(uri).on('complete', function(data, response) {
...
};
return exported;
};
Ora, voglio scrivere unit test per la mia funzione getIssue. 'Restler' è un client REST attraverso il quale effettuo chiamate REST all'API JIRA per ottenere un problema JIRA tramite il mio codice.
Quindi, per essere in grado di testare createIssue (..), voglio essere in grado di prendere in giro la variabile 'resto' nei miei test dell'unità Jasmine.
Come posso prendere in giro questo metodo? Per favore, dammi alcune indicazioni in modo che io possa andare avanti. Ho provato a utilizzare il rewire, ma ho fallito.
Questo è quello che ho finora che non funziona (vale a dire il metodo getIssue risulta essere undefined.):
var rewire = require("rewire");
var EventEmitter = require('events').EventEmitter;
var emitter = new EventEmitter();
var cfg = require("../../../config.js").Configuration;
var jiraModule = rewire("../lib/jira")(cfg);
var sinon = require("sinon");
var should = require("should");
// https://github.com/danwrong/restler
var restMock = {
init : function() {
console.log('mock initiated'+JSON.stringify(this));
},
postJson : function (url, data, options) {
console.log('[restler] POST url='+url+', data= '+JSON.stringify(data)+
'options='+JSON.stringify(options));
emitter.once('name_of_event',function(data){
console.log('EVent received!'+data);
});
emitter.emit('name_of_event', "test");
emitter.emit('name_of_event');
emitter.emit('name_of_event');
},
get : function (url, options) {
console.log('[restler] GET url='+url+'options='+JSON.stringify(options));
},
del : function (url, options) {
console.log('[restler] DELETE url='+url+'options='+JSON.stringify(options));
},
putJson : function (url, data, options) {
console.log('[restler] PUT url='+url+', data= '+JSON.stringify(data)+
'options='+JSON.stringify(options));
}
};
var cfgMock = {
"test" : "testing"
};
jiraModule.__set__("rest", restMock);
jiraModule.__set__("cfg", cfgMock);
console.log('mod='+JSON.stringify(jiraModule.__get__("rest")));
describe("A suite", function() {
it("contains spec with an expectation", function() {
restMock.init();
restMock.postJson(null, null, null);
console.log(cfg.jira);
// the following method turns out to be undefined but when i console.log out the jiraModule, i see the entire code outputted from that file
jiraModule.getIssue("SRMAPP-130", function (err, result) {
console.log('data= '+JSON.stringify(result));
});
expect(true).toBe(true);
});
});
Se qualcuno mi può guidare su come prendere in giro il 'resto' richiede la dipendenza & unit test questo metodo che sarà molto utile.
Inoltre, come dovrei prendere in giro il "conf" passato a module.exports?
grazie
Jasmine-nodo è etichettato, perché stai usando moka? –
Opps, non me ne sono accorto, ma il deridere la dipendenza non ha nulla a che fare con il framework di test. – sarbbottam
ciao, grazie per la guida !! come faccio a passare il conf se sto usando proxyquire? Ho bisogno di prendere in giro anche conf ... che è preso come argomento da module.exports .. – Rookie