2014-10-17 7 views
3

Я новичок в Jasmine/Angular testing и пытается проверить контроллер, который у меня есть. Код контроллера приведен нижеИспытание углового контроллера Как с Jasmine

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

    } 
} 

})();

я написал тест, как показано ниже

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

    }); 

}); 

});

Но я получаю сообщение об ошибке, как

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

Проверочный код. – dfsq

+0

@dfsq извинения, тест не был скопирован правильно. Это тестовый код, добавленный к вопросу сейчас – Junaid

ответ

1

Я получал ошибку, потому что мой введенный объект log не имел функции error. Исправлено это с помощью фиктивного метода в журнале, который принимает один параметр.

-1

Вы должны использовать vm.ActionEvents вместо ActionEvents в строке 24 кода контроллера.

+0

Нет. Это запутанно, но это разные. Injected ActionEvents - это, вероятно, сервис, предоставляющий обещание. – PhiLho