Been working with Sails.js and was having trouble coming up with Jasmine unit tests for a controller. If this is something obvious, please pardon my ignorance, as I've only been deep-diving into JavaScript development for the past 3-4 months.
In past frameworks (specifically ASP .Net MVC), we had libraries to mock out any dependencies a controller might have to, say, an external service (via dependency injection). I kind of wanted to achieve the same level of unit testability with Sails.js so that we achieve a proper "unit" test. Specifically, for my case, I have a controller action with a dependency on a service object -- I simply want to mock the response of that service.
However, I'm having a heck of a time getting this Jasmine unit test to run (using the jasmine-node plugin). My code is below for both the controller and its unit test. What I'm getting right now is:
- The app object doesn't seem to resolve in afterEach()
- The assertions on the spies and the test-level variables are failing.
Is there anything blatantly obvious that I've clearly missed in my unit test? Code below. Thanks for any input!
UserController.js
var Battlefield4Service = require('../services/battlefield4Service');
module.exports = {
/**
* /user/bf4stats
*/
bf4Stats: function (req, res) {
var userName = req.param('userName');
var platform = req.param('platform');
var service = new Battlefield4Service();
service.getPlayerInfo(userName, platform,
function (data) {
// Success callback
res.json(data);
});
}
};
UserController.spec.js
var Sails = require('sails');
var userController = require('./UserController');
var FPSStatsDTO = require('../dto/fpsStatsDTO');
describe('UserController', function() {
// create a variable to hold the instantiated sails server
var app, req, res, rawObject, json;
// Setup mocked dependencies
beforeEach(function() {
// Lift Sails and start the server
Sails.lift({
log: {
level: 'error'
}
}, function(err, sails) {
app = sails;
//done(err, sails);
});
// Mocked Battlefield4Service
Battlefield4Service = {
getPlayerInfo: function (userName, platform, success) {
var dto = new FPSStatsDTO();
dto.userName = userName;
dto.platform = platform;
success(dto);
}
};
// req and res objects, mock out the json call
req = {
param: function(paramName) {
switch (paramName) {
case 'userName':
return 'dummyUser';
case 'platform':
return 'dummyPlatform';
}
}
};
res = {
json: function(object) {
rawObject = object;
json = JSON.stringify(object);
return json;
}
};
// Deploy 007
spyOn(req, 'param');
spyOn(res, 'json');
spyOn(Battlefield4Service, 'getPlayerInfo');
});
afterEach(function(){
app.lower();
});
it('Should call the Battlefield 4 Service', function() {
// Call the controller
userController.bf4Stats(req, res);
// Assertions
expect(req.param).toHaveBeenCalled();
expect(res.json).toHaveBeenCalled();
expect(Battlefield4Service.getPlayerInfo).toHaveBeenCalledWith(req.param('userName'), req.param('platform'));
expect(rawObject.userName).toEqual(req.param('userName'));
expect(rawObject.platform).toEqual(req.param('platform'));
expect(json).toNotBe(null);
expect(json).toNotBe(undefined);
});
});