节点jsunit testing:嘲笑需要依赖

我正在为以下设置编写unit testing的问题作为jira.js文件(在node.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函数编写unit testing。 'restler'是一个REST客户端,我通过REST客户端调用JIRA API来获取JIRA问题。

所以为了能够testingcreateIssue(..),我希望能够在我的Jasmineunit testing中嘲笑'rest'var。

我怎样才能嘲笑这种方法? 请给我一些指示,以便我可以继续。 我曾尝试使用rewire,但我失败了。

这是我迄今为止不起作用(即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); }); }); 

如果有人可以指导我如何嘲笑'rest'需要依赖和unit testing这个方法将是非常有帮助的。

另外,我应该如何嘲笑传递给module.exports的'conf'?

谢谢

你可以使用proxyquire或mockery来存根/模拟依赖关系。

在下面的例子中,我使用了proxyquire 。 希望能帮助到你。


 /* ./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" } }