2015-12-05 25 views
2

我想存根sail控制器函数,但我不知道哪个对象要存根。 使用 sinon.stub(object,'funcname', function()...sails.js + mocha + supertest + sinon:如何将sails.js控制器函数存根

这可能与帆结合控制器功能的方式......

下面是一些代码,得到实施例

控制器文件API /控制器/ PersonController.js

var fs = require('fs'); 

// 
// I want to stub retrieveData function when testing 
// 
function retreiveData(cb) { 
    fs.readFile('./filedata', function (err, data) { 
     if (err) throw err; 
     cb(data.toString()); 
    }); 
}; 
function showdata(req, res) { 
    var stack = new Error().stack 
    console.log(stack) 

    retreiveData(function (data) { 
     res.send(data); 
    }); 
}; 
module.exports = { 
    showdata: showdata, 
    retreiveData: retreiveData 
}; 

测试文件:

var request = require('supertest'); 
var sinon = require('sinon'); 
describe('GET /person/showdata', function() { 
    it('should return person show data', function(done) { 
     // 
     // here is the stub function I want to create 
     // 
     stub = sinon.stub(sails.middleware.controllers.person, 'retreivedata', function(cb) { 
      cb("Some stub data"); 
     }); 
     request(server) 
      .get('/person/showdata') 
      .expect(200) 
      .expect(/Some stub data/) 
      .end(function(err, res) { 
       if (err) 
        throw err; 
       done(); 
      }); 
    }); 
}); 

引导文件:测试/ bootstarp.test.js

var Sails = require('sails'), sails; 
var _ = require('lodash'); 
before(function(done) { 
    Sails.lift({ 
    // configuration for testing purposes 
    }, function(err, s) { 
    if (err) return done(err); 
    sails = s; 
    global.server = sails.hooks.http.app; 
    // here you can load fixtures, etc. 
    done(err, sails); 
    }); 
}); 
after(function(done) { 
    // here you can clear fixtures, etc. 
    sails.lower(done); 
}); 

我正在试验用:

NODE_ENV=test mocha test/bootstrap.test.js test/api/**/*.js 

,并得到:

TypeError: Attempted to wrap object property retreivedata as function 
+0

[存根与Sinon.js一个类的方法]的可能的复制(http://stackoverflow.com/questions/21072016/stubbing-a-class- method-with-sinon-js) – m90

回答

1

这里是一个可能的解决方案。

  • 控制器从ctrlFunc对象调用函数

    var ctrlFunc = { 
        retreiveData: retreiveData, 
    }; 
    function showdata(req, res) { 
         ctrlFunc.retreiveData(function (data) { 
         res.send(data); 
        }); 
    }; 
    
  • 控制器需要测试期间导出ctrlFunc对象(sinon.stub需要访问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,然后存根方法PersonControllerctrlFunc对象

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

放在一起,我们现在有:

  • 控制器文件

    // 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/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(); 
          }); 
        }); 
    }); 
    
  • 测试是现在全成

    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) 
    
+0

感谢您分享。你应该接受你自己的答案。这太糟糕了,没有模块可以做到这一点。 –