【问题标题】:Unit testing an endpoint controller with Node and Rewire使用 Node 和 Rewire 对端点控制器进行单元测试
【发布时间】:2016-10-19 21:48:12
【问题描述】:

我正在使用rewire 来测试我的节点控制器。我有以下端点,它使用request 来获取一些数据。

    exports.myGetEndpoint = function(req, res) {
      return request.get({
        url: 'http://baseURL/api/myGetEndpoint',
        headers: {
          authorization: { //etc }
        },
        json: true
      })
      .then(function(data) {
        res.status(200).json(data.objects);
      });
    };

我想测试当我从控制器调用 get 方法时,request 会使用正确的参数被调用,但我不知道如何根据请求“存根”或“间谍”。

    var Promise    = require('bluebird');
    var rewire     = require('rewire');
    var controller = rewire('../../controllers/myGetEndpoint.js');

    var request = {
      get: sinon.stub()
    };

    // Use rewire to mock dependencies
    controller.__set__({
      request: request
    });

    describe('myGetEndpoint', function() {
      var json;
      var req;
      var res;

      beforeEach(function(done) {
        json = sinon.spy();
        req = { headers: {} };
        res = {
          status: sinon.stub().returns({
            json: json
          })
        };
        controller.myGetEndpoint(req, res).then(function() {
          done();
        });
      });

      it('should call the correct request arguments', function() {
        // lost
      });
    });

【问题讨论】:

    标签: node.js unit-testing express


    【解决方案1】:

    我将修改您的源代码以仅使用回调而不是承诺。我需要研究如何使用 sinon 存根 promise,但您可以了解如何使用简单的回调对其进行测试:

    exports.myGetEndpoint = function(req, res) {
      return request.get({
        url: 'http://baseURL/api/myGetEndpoint',
        headers: {
          authorization: { //etc }
        },
        json: true
      }, function (error, response, body) {
        res.status(200).json(response.objects);
      });
    };
    

    您不应该在 beforeEach 中调用控制器,但必须在测试用例中调用它。 beforeEach 通常用于在测试用例之前进行初始化:

    var expect = require('chai').expect;
    var Promise    = require('bluebird');
    var rewire     = require('rewire');
    var controller = rewire('../../controllers/myGetEndpoint.js');
    
    var request = {
      get: sinon.stub()
    };
    
    // Use rewire to mock dependencies
    controller.__set__({
      request: request
    });
    
    describe('myGetEndpoint', function() {
      var status;
      var json;
      var req;
      var res;
    
      beforeEach(function(done) {
        json = sinon.spy();
        status = sinon.stub();
        req = { headers: {} };
        res = {
          status: status.returns({
            json: json
          })
        };
      });
    
      it('should call the correct response arguments', function(done) {
        var response = {objects: ['objects', 'objects']}
        request.get.yields(null, response);
    
        controller.myGetEndpoint(req, res);
    
        expect(status.calledWith(200)).to.equal(true);
        expect(json.calledWith(response.objects)).to.equal(true);
      });
    });
    

    P.S.:我实际上并没有尝试运行它,所以可能有一些拼写错误。

    【讨论】:

      猜你喜欢
      • 2017-04-26
      • 2017-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-01
      • 2019-12-31
      • 2013-03-09
      • 1970-01-01
      相关资源
      最近更新 更多