मैं (एक Node.js मॉड्यूल में) एक jira.js फ़ाइल के रूप में निम्नलिखित सेटअप के लिए इकाई परीक्षण लेखन मुद्दों कर रहा हूँ:नोड js इकाई परीक्षण: मजाक की आवश्यकता होती है निर्भरता
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;
};
अब, मैं लिखना चाहते हैं मेरे getIssue फ़ंक्शन के लिए यूनिट परीक्षण। 'रेस्टलर' एक आरईएसटी क्लाइंट है जिसके माध्यम से मैं अपने कोड के माध्यम से एक जेआईआरए मुद्दा प्राप्त करने के लिए जेआईआरए एपीआई को आरईएसटी कॉल करता हूं।
तो createIssue (..) का परीक्षण करने में सक्षम हो, मैं अपने जैस्मीन इकाई परीक्षण में 'आराम' var उपहास करने के लिए सक्षम होना चाहते हैं।
कैसे मैं इस विधि नकली कर सकते हैं? कृपया मुझे कुछ पॉइंटर्स दें ताकि मैं आगे जा सकूं। मैंने रिवायर का उपयोग करने की कोशिश की है लेकिन मैं असफल रहा हूं। (। यानी getIssue विधि पता चला है अपरिभाषित जा करने के लिए):
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);
});
});
कोई कैसे 'आराम' उपहास करने के लिए मुझे मार्गदर्शन कर सकते हैं निर्भरता & की आवश्यकता होती है
यह मैं अब तक जो काम नहीं करता क्या है इकाई इस विधि का परीक्षण करें जो बहुत उपयोगी होगी।
इसके अलावा, मैं कैसे नकली चाहिए 'conf' module.exports को पारित किया जा रहा?
धन्यवाद
जैस्मीन नोड टैग है, कारण है कि आप मोचा उपयोग कर रहे हैं? –
ओह, मुझे लगता है कि नोटिस नहीं किया था, लेकिन मजाक निर्भरता परीक्षण ढांचे के साथ कोई संबंध नहीं है। – sarbbottam
हैलो, मार्गदर्शन के लिए धन्यवाद !! अगर मैं प्रॉक्सीक्वायर का उपयोग कर रहा हूं तो मैं conf में कैसे पास करूं? मैं भी नकली conf .... जो module.exports द्वारा एक तर्क के रूप में लिया जाता है की जरूरत है .. – Rookie