I am a beginner in Jasmine/Angular testing and I'm attempting to test a controller that I have created. The code for the controller is shown below:
(function () {
'use strict';
angular
.module('App')
.controller('ActionEventsCtrl', ActionEventsCtrl);
ActionEventsCtrl.$inject = ['$log', 'ActionEvents'];
function ActionEventsCtrl($log, ActionEvents) {
/* jshint validthis:true */
var vm = this;
vm.ActionEvents = [
{ "description": "Second Notification", "type": 4, "dateRaised": "2014-10-17T00:00:00", "hasNotified": true, "status": 0, "user": null, "id": 6 },
{ "description": "Third Notification", "type": 2, "dateRaised": "2014-10-18T00:00:00", "hasNotified": true, "status": 1, "user": null, "id": 7 }
];
vm.init = getActionEvents();
function getActionEvents() {
var userId = 1;
ActionEvents.get(userId).then(
function onSuccess(response) {
vm.ActionEvents = response.data;
},
function onFailure(response) {
$log.error("Loading of ActionEvents failed with response: ", response);
});
}
}
})();
In order to test this controller, I've written the following test:
describe("App", function () {
describe("ActionEvents Controller", function () {
//basic mock lookup service which returns empty arrays
var mockActionEventsService = {
get: function (userId) {
return [
{ "description": "Second Notification", "type": 4, "dateRaised": "2014-10-17T00:00:00", "hasNotified": true, "status": 0, "user": null, "id": 6 },
{ "description": "Third Notification", "type": 2, "dateRaised": "2014-10-18T00:00:00", "hasNotified": true, "status": 1, "user": null, "id": 7 }
];
},
};
var scope;
var log;
var controller;
beforeEach(module('TracerApp'));
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
log = null;
controller = $controller('ActionEventsCtrl as actionEventVM', { $log: log, ActionEvents: mockActionEventsService });
}));
it('should ensure data is present', function () {
expect(scope.actionEventVM.ActionEvents).toJson();
});
});
Unfortunately, when running the tests, I encountered the following error message:
TypeError: undefined is not a function
TypeError: undefined is not a function
at getActionEvents ( actionevents.controller.js: line 24:38)
at new ActionEventsCtrl ( actionevents.controller.js: line 20:19)
...