2014-10-17 95 views
3

我是新来的茉莉花/角测试,并试图测试我有一个控制器。控制器代码如下测试角度控制器与茉莉花一样

(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 2014-10-17 10:58:51

+0

@dfsq道歉,测试没有被正确复制。这是测试代码现在添加到这个问题 – Junaid 2014-10-17 11:02:20

回答

1

我得到错误,因为我注入的log对象没有error函数。通过在采用一个参数的日志中使用虚拟方法来修复它。

-1

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

+0

不。这是令人困惑的,但这些是不同的。注入的ActionEvents可能是暴露承诺的服务。 – PhiLho 2015-10-15 12:24:09