Testing Angular Controller Like Jasmine

I am new to Jasmine / Angular testing and am trying to test the controller I have. Controller code below

(function () {
'use strict';

angular
    .module('App')
    .controller('ActionEventsCtrl', ActionEventsCtrl);

ActionEventsCtrl.$inject = ['$log', 'ActionEvents'];

function ActionEventsCtrl($log, ActionEvents) {
    /* jshint validthis:true */
    var vm = this;
    //getActionEvents();

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

    }
}

      

}) ();

I wrote a test below

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 have data', function () {
        expect(scope.actionEventVM.ActionEvents).toJson();

    });

});

      

});

But I am getting an error like

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)
at d (  angular.min.js: line 35:36)
at Object.instantiate (  angular.min.js: line 35:165)
at   angular.min.js: line 67: line 419
at null.<anonymous> ( actionevents.controller.test.js: line 34:26)
at Object.d [as invoke] (  angular.min.js: line 35:36)
at workFn (  angular-mocks.js: line 2161:20)
at jasmine.Block.execute (  jasmine.js: line 1064:17)
at jasmine.Queue.next_ (  jasmine.js: line 2096:31)
Error: Declaration Location
at window.inject.angular.mock.inject (  angular-mocks.js: line 2146:25)
at null.<anonymous> ( actionevents.controller.test.js: line 31:20)
at jasmine.Env.describe (  jasmine.js: line 819:21)
at describe (  jasmine.js: line 603:27)
at null.<anonymous> ( actionevents.controller.test.js: line 12:5)
at jasmine.Env.describe (  jasmine.js: line 819:21)
at describe (  jasmine.js: line 603:27)
at  actionevents.controller.test.js: line 10:1TypeError: Cannot read property 'ActionEvents' of undefined
 TypeError: Cannot read property 'ActionEvents' of undefined
at null.<anonymous> ( actionevents.controller.test.js: line 39:39)
at jasmine.Block.execute (  jasmine.js: line 1064:17)
at jasmine.Queue.next_ (  jasmine.js: line 2096:31)
at   jasmine.js: line 2086:18

      

+3


source to share


2 answers


I was getting the error because my injected object log

had no function error

. Fixed this with a dummy log method that takes one parameter.



+1


source


You should use vm.ActionEvents instead of ActionEvents on line 24 of your controller code.



-1


source







All Articles