测试角度控制器和Jasmine一样

时间:2014-10-17 10:56:22

标签: javascript angularjs unit-testing jasmine

我是Jasmine / Angular测试的新手,并尝试测试我拥有的控制器。控制器代码如下

(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

2 个答案:

答案 0 :(得分:1)

我收到错误是因为我注入的log对象没有error功能。通过在日志中使用一个带有一个参数的虚方法来修复它。

答案 1 :(得分:-1)

您应该在控制器代码的第24行使用vm.ActionEvents而不是ActionEvents。