I am trying to stub a sails controller function, but I don't know which object to stub.
using
sinon.stub(object,'funcname', function()...
This is probably related to the way sails bind controller functions...
Here is some code to give example
Controller file api/controllers/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
};
Test file:
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();
});
});
});
bootstrap file: test/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);
});
I am running test with:
NODE_ENV=test mocha test/bootstrap.test.js test/api/**/*.js
and get :
TypeError: Attempted to wrap object property retreivedata as function
Here is a possible solution.
Controller call the function from a ctrlFunc object
Controller need to export ctrlFunc object during test (sinon.stub need access to ctrlFunc)
test file require PersonController, then stub method on PersonController.ctrlFunc object
placing all together we have now:
controller file
test file:
test is now successfull