Node.js单元测试:模拟“require”依赖

8

我遇到了编写单元测试的问题,以下是一个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函数编写单元测试。'restler'是一个REST客户端,通过它我可以通过我的代码调用JIRA API来获取JIRA问题。
因此,为了能够测试createIssue(..),我希望能够在Jasmine单元测试中模拟'rest'变量。
我应该如何模拟这个方法?请给我一些指针,以便我可以继续前进。我尝试过使用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' require依赖项并对这种方法进行单元测试,那将非常有帮助。
另外,我应该如何模拟传递给module.exports的'conf'?
谢谢。
1个回答

7
你可以使用proxyquiremockery来存根/模拟依赖项。
在下面的示例中,我使用了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"
  }
}

1
Jasmine-node已经被标记了,你为什么要使用mocha? - Alexandr Lazarev
谢谢!这个解释看起来很有希望。在我尝试并查看它是否有效之前,还有一件事情。我不确定如何触发“complete”事件以进行测试(如果您查看REST调用,它会抛出一个事件)。上面给出的'restlerSTub'存根在这种情况下是否有效?还是我必须使用EventEmitter并从我的restlerStub函数中发出事件? - Rookie
rest.postJson().on() 可以理解为 rest.postJson() 返回一个具有 on 方法的对象 {on: function(){}},随后调用该 on 方法。如果您查看存根,它也是这样做的。您也可以使用 EventEmitter - Sarbbottam
谢谢Sarbbottam。这真是一个好建议!我会测试一下,然后告诉你它是否对我有用。非常感谢你的帮助! - Rookie
太棒了!这个运行得像魔法一样!非常感谢Sarbbottam的建议。你太棒了 :) - Rookie
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接