Angularjs 在jasmine和angular中测试局部json变量
我试着测试我的js代码。在我的角度控制器函数中有一个局部变量和一个局部函数。我的角函数是我需要测试的Angularjs 在jasmine和angular中测试局部json变量,angularjs,jasmine,karma-jasmine,Angularjs,Jasmine,Karma Jasmine,我试着测试我的js代码。在我的角度控制器函数中有一个局部变量和一个局部函数。我的角函数是我需要测试的 angular.module('reasonCode').controller('ReasonCodeController', ReasonCodeController); function ReasonCodeController($location, gettext, reasonCodeConfig, NotifyToastService, reasonCodeModalService,
angular.module('reasonCode').controller('ReasonCodeController', ReasonCodeController);
function ReasonCodeController($location, gettext, reasonCodeConfig, NotifyToastService, reasonCodeModalService,
reasonCodeService, gettextCatalog) {
var ctrl = this;
ctrl.gridKey = reasonCodeConfig.gridKey;
ctrl.defaultColumns = reasonCodeConfig.columns;
ctrl.updateItem = updateItem;
ctrl.reasonCodeData = [];
ctrl.newReasonCode = newReasonCode;
ctrl.actions = {
deleteData: deleteReasonCode,
editData:editReasonData
};
init();
function init() {
ctrl.selectedItem = {};
ctrl.gridInstance = {};
ctrl.reloadGrid = false;
// setReasonCodeToGrid();
}
function deleteReasonCode(data) {
var currentItem = data;
if (!currentItem) {
currentItem = ctrl.selectedItem;
}
var urlParams = { 'reasoncodeId': currentItem.reasonCodeId };
var message = gettextCatalog.getString("Delete {{name}}. Continue?", { name: currentItem.reasonName });
if (currentItem.ruleId.length === 0) {
reasonCodeModalService.promptModal(message).then(function(response) {
if (response === 'ok') {
reasonCodeService.remove(urlParams, {}, {})
.then(function(response) {
NotifyToastService.showApiResponse(response.responseMessages);
ctrl.reasonCodeData.splice(findIndex(currentItem), 1);
refreshReasonCodeGrid();
});
}
});
} else {
var response = [{
messageType: 0,
responseMessage: "Reason Code in use"
}];
NotifyToastService.showApiResponse(response);
}
}
我的茉莉花规格代码是
describe('ReasonCodeController', function() {
beforeEach(module('reasonCode'));
var ctrl;
var rootScope;
var q;
var $timeout;
var reasonCodeConfig;
var NotifyToastService;
var reasonCodeService;
var reasonCodeModalService;
var gettext;
var deferred;
var $scope;
beforeEach(module(function($urlRouterProvider) {
$urlRouterProvider.otherwise(function() {
return false;
});
}));
beforeEach(inject(function($rootScope, $controller, $q, _$timeout_, $state, gettext) {
q = $q;
$scope = {
$on: function() {
}
};
rootScope = {};
$timeout = _$timeout_;
gettext = gettext;
deferred = $q.defer();
reasonCodeConfig = {
gridKey: 'REASONCODE',
columns: []
};
NotifyToastService = {
showApiResponse: function() {}
};
reasonCodeService = {
findAll: function(obj) {},
remove: function(obj) {
}
};
reasonCodeModalService = {
showModal: function(option) {
},
promptModal: function(message) {
}
};
//spy before initilasation of controller so init funct has access to the spy
spyOn(reasonCodeService, 'findAll').and.returnValue(deferred.promise);
ctrl = $controller('ReasonCodeController', {
reasonCodeConfig: reasonCodeConfig,
NotifyToastService: NotifyToastService,
reasonCodeService: reasonCodeService,
reasonCodeModalService: reasonCodeModalService,
$rootScope: rootScope
});
deferred.resolve([{ status: 3, data: [{ reasonCodeId: 123 }] }]);
$rootScope.$apply();
}));
it('deleteReasonCode function if reason code is not default', inject(function($rootScope) {
ctrl.selectedItem = {
ruleId: []
};
var currentItem = ctrl.selectedItem;
spyOn(reasonCodeModalService, 'promptModal').and.callThrough();
spyOn(reasonCodeService, 'remove').and.callFake(function() {
return {
then: function(callback) {
return callback({ status: 3, responseMessages: [{ message: 'msg' }], data: '' });
}
};
});
spyOn(NotifyToastService, 'showApiResponse');
ctrl.actions.deleteData({});
deferred.resolve('ok');
$rootScope.$apply();
expect(reasonCodeModalService.promptModal).toHaveBeenCalled();
expect(reasonCodeService.remove).toHaveBeenCalled();
expect(NotifyToastService.showApiResponse).toHaveBeenCalled();
}));
});
当我运行代码时,我得到错误currentItem.ruleId.length
未定义。我知道这是函数内部的局部变量。那么我如何重写我的测试呢?
我遵循约翰·帕帕的角度编码风格。我的范围变量包含删除函数 当您将
currentItem
作为参数传递给ctrl.actions.deleteData
时,它是否工作正常?看起来像是ctrl。由于某些原因,无法访问selectedItem
。您能在实例化控制器的地方发布测试样板代码吗?假设ctrl.actions.deleteData({})
调用deleteReasonCode({})
,因为您将empry对象作为参数传递,并且currentItem被初始化为这个empry对象,所以它应该具有currentItem.ruleId
。但是我们需要在此做出假设,因为您没有发布测试调用的代码。很抱歉,这是我的整个控制器。当您将currentItem
作为参数传递到ctrl.actions.deleteData
时,它是否正常工作?看起来像是ctrl。由于某些原因,无法访问selectedItem
。您能在实例化控制器的地方发布测试样板代码吗?假设ctrl.actions.deleteData({})
调用deleteReasonCode({})
,因为您将empry对象作为参数传递,并且currentItem被初始化为这个empry对象,所以它应该具有currentItem.ruleId
。但是我们需要在这里做出假设,因为您没有发布测试调用的代码。很抱歉,这是我的整个控制器