且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

sails.js +摩卡+超级测试+ sinon:如何对sails.js控制器函数进行存根

更新时间:2023-08-19 09:21:19

这是一个可能的解决方案.

Here is a possible solution.

  • 控制器从 ctrlFunc 对象

var ctrlFunc = {
   retreiveData: retreiveData,
};
function showdata(req, res) {
        ctrlFunc.retreiveData(function (data) {
        res.send(data);
    });
};

  • 控制器在测试期间需要导出 ctrlFunc 对象(sinon.stub需要访问 ctrlFunc )

  • 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;
    }
    

  • 测试文件需要 PersonController ,然后在 PersonController 上存根方法. 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");
        });
    

  • 我们现在在一起:

    • 控制器文件

    • 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)