gpt4 book ai didi

javascript - 像 Jasmine 一样测试 Angular Controller

转载 作者:行者123 更新时间:2023-12-03 12:39:59 24 4
gpt4 key购买 nike

我是 Jasmine/Angular 测试的新手,正在尝试测试我拥有的 Controller 。 Controller 代码如下

(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

最佳答案

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

关于javascript - 像 Jasmine 一样测试 Angular Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26423512/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com