Javascript node js单元测试:模拟需要依赖关系
我在将以下设置的单元测试作为jira.js文件(在node.js模块中)编写时遇到问题: 现在,我想为getIssue函数编写单元测试restler'是一个REST客户机,通过它我可以对jiraapi进行REST调用,从而通过我的代码获得JIRA问题 因此,为了能够测试createIssue(..),我希望能够在Jasmine单元测试中模拟“rest”变量 我怎么能嘲笑这种方法?请给我一些建议,这样我就可以继续了。我试过使用重新布线,但失败了 这是迄今为止我所拥有的不起作用的方法(即getIssue方法被证明是未定义的): 如果有人能指导我如何模拟“rest”需要依赖关系和单元测试,那么这个方法将非常有用 另外,我应该如何模拟传递给module.exports的'conf' 谢谢您可以使用或存根/模拟依赖项 在下面的示例中,我使用了。希望能有帮助Javascript node js单元测试:模拟需要依赖关系,javascript,node.js,mocking,sinon,jasmine-node,Javascript,Node.js,Mocking,Sinon,Jasmine Node,我在将以下设置的单元测试作为jira.js文件(在node.js模块中)编写时遇到问题: 现在,我想为getIssue函数编写单元测试restler'是一个REST客户机,通过它我可以对jiraapi进行REST调用,从而通过我的代码获得JIRA问题 因此,为了能够测试createIssue(..),我希望能够在Jasmine单元测试中模拟“rest”变量 我怎么能嘲笑这种方法?请给我一些建议,这样我就可以继续了。我试过使用重新布线,但失败了 这是迄今为止我所拥有的不起作用的方法(即getIss
Jasmine节点被标记了,你为什么要使用mocha?哎呀,我没有注意到,尽管模仿依赖关系与测试框架无关。你好,谢谢你的指导!!如果使用proxyquire,如何传递conf?我还需要模拟conf…,module.exports将其作为参数。
var index=proxyquire(“../src/index”,{'restler':restlerStub})(conf)代码>谢谢!这一解释看起来很有希望。在我尝试之前还有一件事,看看这是否有效。我不知道如何触发“complete”事件进行测试(如果查看rest调用,它会抛出一个事件)。在这种情况下,上述“restlestub”的存根有效吗?或者我必须使用eventemitter并从restleStub函数发出事件吗?
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;
};
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);
});
});
/* ./src/index.js */
var rest = require('restler');
module.exports = function (conf) {
var exported = {};
exported.getIssue = function (issueId, done) {
var uri = '';
var reqObj = '';
var service = {
auth : ''
};
rest.postJson(uri, reqObj, service.auth).on('complete', function(data, response) {
done(data, response);
});
};
return exported;
};
/* ./test/index.js */
var proxyquire = require('proxyquire');
var assert = require('chai').assert;
var restlerStub = {
postJson: function() {
return {
on: function(event, callback) {
callback('data', 'response');
}
}
}
}
var index = proxyquire('../src/index', {'restler': restlerStub})();
describe('index', function() {
it('should return the desired issue', function(done) {
var issue = index.getIssue('issueId', function(data, response) {
assert.equal(data, 'data');
assert.equal(response, 'response');
done();
})
});
});
/* ./package.json */
{
"scripts": {
"test": "mocha"
},
"dependencies": {
"restler": "^3.4.0"
},
"devDependencies": {
"chai": "^3.4.1",
"mocha": "^2.3.4",
"proxyquire": "^1.7.3"
}
}