Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 在jasmine和angular中测试局部json变量_Angularjs_Jasmine_Karma Jasmine - Fatal编程技术网

Angularjs 在jasmine和angular中测试局部json变量

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,

我试着测试我的js代码。在我的角度控制器函数中有一个局部变量和一个局部函数。我的角函数是我需要测试的

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
。但是我们需要在这里做出假设,因为您没有发布测试调用的代码。很抱歉,这是我的整个控制器