sails.js + mocha + supertest + sinon: how to stub sails.js controller function

僤鯓⒐⒋嵵緔 提交于 2019-12-05 14:17:05

Here is a possible solution.

  • Controller call the function from a ctrlFunc object

    var ctrlFunc = {
       retreiveData: retreiveData,
    };
    function showdata(req, res) {
            ctrlFunc.retreiveData(function (data) {
            res.send(data);
        });
    };
    
  • Controller need to export ctrlFunc object during test (sinon.stub need access to ctrlFunc)

    /*
      Only add extra exports during test
      this allow sinon.stub to retreive object during test
    */
    
    if (process.env.NODE_ENV === 'test') {
        module.exports.ctrlFunc = ctrlFunc;
    }
    
  • test file require PersonController, then stub method on PersonController.ctrlFunc object

        var PersonCtrl = require('../../../api/controllers/PersonController');
        stub = sinon.stub(PersonCtrl.ctrlFunc, 'retreiveData', function(cb) {
          console.log('into stub function');
          cb("Some stub data");
        });
    

placing all together we have now:

  • controller file

    // File: api/controllers/PersonController.js
    var fs = require('fs');
    var ctrlFunc = {
        retreiveData: retreiveData,
    };
    function retreiveData (cb) {
        fs.readFile('./filedata', function (err, data) {
            if (err) throw err;
            cb(data.toString());
        });
    };
    
    function showdata(req, res) {
            ctrlFunc.retreiveData(function (data) {
            res.send(data);
        });
    };
    
    module.exports = {
      showdata: showdata,
    };
    
    /*
      Only add extra exports during test
      this allow sinon.stub to retreive object during test
    */
    
    if (process.env.NODE_ENV === 'test') {
        module.exports.ctrlFunc = ctrlFunc;
    }
    
  • test file:

    // test/api/controllers/PersonController.test.js
    var request = require('supertest');
    var sinon = require('sinon');
    
    describe('GET /person/showdata', function() {
        var stub;
        before(function() {
            var PersonCtrl = require('../../../api/controllers/PersonController');
            stub = sinon.stub(PersonCtrl.ctrlFunc, 'retreiveData', function(cb) {
              console.log('into stub function');
              cb("Some stub data");
            });
    
        });
        after(function() {
            stub.restore();
        });
        it('should return person show data', function(done) { 
            request(server)
                .get('/person/showdata')
                .expect(200)
                .expect(/Some stub data/)
                .end(function(err, res) {
                    if (err)
                        throw err;
                    done();
                });
        });
    });
    
  • test is now successfull

    NODE_ENV=test mocha test/bootstrap.test.js test/api/controllers/PersonController.test.js
        GET /person/showdata
        into stub function
            ✓ should return person show data (62ms)
          1 passing (2s)
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!