单元测试中的$ digest已经在进行jasmin

时间:2018-02-07 11:15:41

标签: angularjs karma-jasmine

我有下面的控制器

(function () {
'use strict';

angular.module('fccui')
    .controller('OfferPricingCtrl', ['$rootScope','$scope', '$mdDialog', 'Constants', '$localStorage', '$log','rest','$q','catalog',
        function ($rootScope,$scope, $mdDialog, Constants, $localStorage, $log,REST,$q,catalog) {
            var vm = this;
            vm.currencies = Constants.Currencies;
            $log.debug('dans OfferPricingCtrl');
            vm.dosort = false;
            vm.togs = typeof $localStorage.togs !== 'undefined' ? $localStorage.togs : {
                offer: true
            };
            catalog.get('config').then(function (e) {
                vm.config = e;
            });
            vm.toggle = function (w,id) {
                var deferred=$q.defer();
                if(w !== 'offer' && vm.togs[id] === undefined && vm.config.appDataVolume !== 'lite'){
                    $rootScope.OFFER.selectedProducts[w].description.currentStep=['UIStep',Constants.OfferSteps[7].name];
                    $rootScope.OFFER.step=['UIStep',Constants.OfferSteps[7].name];
                    $rootScope.OFFER.currentStep=['UIStep',Constants.OfferSteps[7].name];
                    REST.currentOffer.update($rootScope.OFFER,
                    function(e){
                        console.debug('inside success',e);
                        $scope.$parent.main.offerReceived(e);
                        vm.togs[id] = true;
                        $localStorage.togs = vm.togs;
                        deferred.resolve(vm.togs);
                    },function(z){
                        deferred.reject(z);
                    });
                }else{
                    vm.togs[id] = typeof vm.togs[id] === 'undefined' ? true : !vm.togs[id];
                    $localStorage.togs = vm.togs;
                    deferred.resolve(vm.togs);
                }
                return deferred.promise;
            };
            vm.addCharge = function (ev, product, charges) {
                $log.debug('addCharge', product, charges);
                var newCharge = {
                    isnew: true,
                    name: '',
                    type: 'OneTime',
                    segments: [],
                    currency: $localStorage.config.defaultCurrency,
                    characteristics: [],
                    pricingProductGroups: [],
                    startUseBill: false,
                    startBill: 1,
                    endUseBill: false,
                    endBill: 1,
                    pricePlan: null
                };
                $log.debug('addCharge -->', product, charges, newCharge);

                $mdDialog.show({
                    controller: 'PricingDialogCtrl',
                    controllerAs: 'ctrl',
                    templateUrl: 'views/pricing-dialog.html',
                    parent: angular.element(document.body),
                    targetEvent: ev,
                    clickOutsideToClose: false,
                    fullscreen: true,
                    locals: {
                        offer: $rootScope.OFFER,
                        product: product,
                        plan: null,
                        charge: newCharge,
                        discount: null
                    }
                }).then(function (charge) {
                    charges.push(charge);
                    $scope.$parent.main.moveToStep();
                });
            };
        }
    ]);

我的测试用例如下

    var offer = {
     'selectedProducts': [{
     'description': {
     'productId': 'offer_1_20',
     'productIdExt': null,
     'fcState': null,
     'lifecycleStatus': null,
     'name': 'name',
     'description': 'sd',
     'type': 'PO',
     'status': 'Draft',
     'createdDate': 1517824890573,
     'modifiedDate': 1517824890573,
     'version': 2,
     'subType': '',
     'owner': 'N/A',
     'recordUse': null,
     'currentStep': null
   }};

   var offerReceived = {
     'selectedProducts': [{
     'description': {
       'productId': 'offer_1_20',
       'productIdExt': null,
       'fcState': null,
       'lifecycleStatus': null,
       'name': 'name',
       'description': 'sd',
       'type': 'PO',
       'status': 'Draft',
       'createdDate': 1517824890573,
       'modifiedDate': 1517824890573,
       'version': 2,
       'subType': '',
       'owner': 'N/A',
       'recordUse': null,
       'currentStep': null
    },
    'optional': false,
    ' ruleMap': {
      'Required': [],
      'Incompatible': [],
      'Autoprovisioned': []
   },
   'pricing': {
     'charges': [],
     'pricePlans': [{
      'amount': null,
      'currency': null,
      'plan': null,
      'name': '-1',
      'priority': 1,
      'productId': 'offer_1_20_PricePlan_1517820131689',
      'fcState': null,
      'productIdExt': '',
      'description': null,
      'startDate': null,
      'endDate': null,
      'forceEdition': null,
      'characteristics': [],
      'segments': [],
      'pricingProductGroups': [],
      'type': 'Composite',
      'minAmount': null,
      'chargeFrequency': null,
      'chargeFrequencyUOM': null,
      'chargePer': null,
      'chargePerUOM': null,
      'chargeUOM': null,
      'chargeMinBoundary': null,
      'chargeMaxBoundary': null,
      'discounts': [],
      'subPrices': [{
        'amount': '5.00',
        'currency': 'USD',
        'plan': '-1',
        'name': 'charge_name',
        'priority': 1,
        'productId': 'offer_1_20_PricePlan_1517820131689UIPricecharge_name-1517820131691',
        'fcState': null,
        'productIdExt': '',
        'description': null,
        'startDate': 1518978600000,
        'endDate': 1518978600000,
        'forceEdition': null,
        'characteristics': [],
        'segments': [],
        'pricingProductGroups': [],
        'type': 'OneTime',
        'minAmount': '0.00',
        'chargeFrequency': '1.00',
        'chargeFrequencyUOM': null,
        'chargePer': null,
        'chargePerUOM': null,
        'chargeUOM': null,
        'chargeMinBoundary': null,
        'chargeMaxBoundary': null,
        'discounts': [],
        'subPrices': []
      }]
    }]
  }
}],
'pricing': {
  'charges': [],
  'pricePlans': [{
    'amount': null,
    'currency': null,
    'plan': null,
    'name': '-1',
    'priority': 1,
    'productId': 'offer_1_1_PricePlan_1517811979018',
    'fcState': null,
    'productIdExt': '',
    'description': null,
    'startDate': null,
    'endDate': null,
    'forceEdition': null,
    'characteristics': [],
    'segments': [],
    'pricingProductGroups': [],
    'type': 'Composite',
    'minAmount': null,
    'chargeFrequency': null,
    'chargeFrequencyUOM': null,
    'chargePer': null,
    'chargePerUOM': null,
    'chargeUOM': null,
    'chargeMinBoundary': null,
    'chargeMaxBoundary': null,
    'discounts': [],
    'subPrices': [{
      'amount': '4.00',
      'currency': 'USD',
      'plan': '-1',
      'name': 'charge_offer_1_1',
      'priority': 1,
      'productId': 'offer_1_1_PricePlan_1517811979018UIPricecharge_offer_1_1-1517811979059',
      'fcState': null,
      'productIdExt': '',
      'description': null,
      'startDate': 1517769000000,
      'endDate': 1518373800000,
      'forceEdition': '1517812000384',
      'characteristics': [],
      'segments': [],
      'pricingProductGroups': [],
      'type': 'OneTime',
      'minAmount': '1.00',
      'chargeFrequency': '1.00',
      'chargeFrequencyUOM': null,
      'chargePer': null,
      'chargePerUOM': null,
      'chargeUOM': null,
      'chargeMinBoundary': null,
      'chargeMaxBoundary': null,
      'discounts': [],
      'subPrices': []
    }]
  }]
},
'characteristics': [],
'salesChannel': [],
'accessedUIStepList': [
  ['UIStep', 'Products'],
  ['UIStep', 'Pricing']
];

    describe('Controller: OfferPricingCtrl test toggle function with saved 
  togs', function () {

// load the controller's module
beforeEach(module('fccui'));

var OfferPricingCtrl, storage, scope, $httpBackend;


afterEach(function () {
  // Ensure that all expects set on the $httpBackend
  // were actually called
  //$httpBackend.verifyNoOutstandingExpectation();
  // Ensure that all requests to the server
  // have actually responded (using flush())
  //$httpBackend.verifyNoOutstandingRequest();
});

// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope, _$localStorage_, _$httpBackend_) {
  storage = _$localStorage_;
  $httpBackend = _$httpBackend_;
  storage.togs = {
    yep: true
  };
  $rootScope.OFFER = offer;
  scope = $rootScope.$new();
  scope.$parent = {
    main: {
      offer: {
        productId: 427
      },
      moveToStep: function () {},
      offerReceived: function () {}
    }
  };

  $httpBackend.whenGET('http://server:80/appconfig').respond({
    'singleOfferRecordType': [
      'PO'
    ],
    'bundleOfferRecordType': [
      'BPO'
    ],
    'autoprovisionedTypes': [
      'CFS'
    ],
    'ruleTypes': [
      'Eligible',
      'InEligible',
      'InCompatible'
    ],
    'offerStatus': [
      'Draft',
      'PendingApproval',
      'Active',
      'Disabled'
    ],
    'offerSubType': [
      'Device',
      'Internet',
      'TV'
    ],
    'defaultDateFormat': 'MM/dd/yyyy',
    'defaultTimeFormat': 'HH:mm:ss.S',
    'defaultTimestampFormat': 'yyyy-MM-dd HH:mm:ss.S',
    'priceBoundaryStartsAtZero': true,
    'priceBoundaryMinIncluded': true,
    'priceBoundaryMaxIncluded': true,
    'defaultCurrency': 'USD',
    'numericOperator': [
      '<',
      '<=',
      '>',
      '>=',
      '=',
      '<>'
    ],
    'booleanOperator': [
      'True',
      'False'
    ],
    'stringOperator': [
      'IS',
      'IS NOT'
    ],
    'conditionImplementationType': [
      'INVENTORY',
      'SEGMENT'
    ],
    'inventoryOperator': [
      'CONTAINS',
      'NOT CONTAINS'
    ],
    'inventoryProductTypes': [
      'DEVICE'
    ],
    'appDataVolume': 'heavy'
  });
  OfferPricingCtrl = $controller('OfferPricingCtrl', {
    $scope: scope
    // place here mocked dependencies
  });
  $httpBackend.flush();
}));

it('test toggle function (with history)', function () {
  expect(OfferPricingCtrl.togs).toEqual({
    yep: true
  });
  expect(storage.togs).toEqual({
    yep: true
  });
  $httpBackend.whenPUT('http://server:80/work/offer/current').respond(offerReceived);
  OfferPricingCtrl.toggle(0, 'yo');
  $httpBackend.flush();
  console.log('storage.togs', storage.togs);
  expect(storage.togs.yo).toEqual(true);
  OfferPricingCtrl.toggle(0, 'yo');
  $httpBackend.flush();
  console.log('storage.togs', storage.togs);
  expect(storage.togs.yo).toEqual(false);
});

我在$digest进程正在进行中时遇到错误

  

TypeError:undefined不是对象(评估'current。$$ nextSibling')(第17864行)       $ @消化E:/cim/branches/OPD_401_EB/src/fc-commercial-frontend/bower_components/angular/angular.js:17864:60       冲洗@ E:/cim/branches/OPD_401_EB/src/fc-commercial-frontend/bower_components/angular-mocks/angular-mocks.js:1806:45       E:/cim/branches/OPD_401_EB/src/fc-commercial-frontend/test/spec/controllers/offer-pricing.js:782:25       调用@ E:/cim/branches/OPD_401_EB/src/fc-commercial-frontend/bower_components/angular/angular.js:4771:24       WorkFn @ E:/cim/branches/OPD_401_EB/src/fc-commercial-frontend/bower_components/angular-mocks/angular-mocks.js:3130:26       注入@ E:/cim/branches/OPD_401_EB/src/fc-commercial-frontend/bower_components/angular-mocks/angular-mocks.js:3097:28       E:/cim/branches/OPD_401_EB/src/fc-commercial-frontend/test/spec/controllers/offer-pricing.js:698:22       E:/cim/branches/OPD_401_EB/src/fc-commercial-frontend/test/spec/controllers/offer-pricing.js:680:11       全球代码@E:/cim/branches/OPD_401_EB/src/fc-commercial-frontend/test/spec/controllers/offer-pricing.js:805:3       TypeError:undefined不是E:/cim/branches/OPD_401_EB/src/fc-commercial-frontend/app/scripts/controllers/offer-pricing.js中的对象(评估'vm.config.appDataVolume')(第9行)       拨动@ E:/cim/branches/OPD_401_EB/src/fc-commercial-frontend/app/scripts/controllers/offer-pricing.js:9:1412       E:/cim/branches/OPD_401_EB/src/fc-commercial-frontend/test/spec/controllers/offer-pricing.js:793:30

我在执行$httpBackend.flush()方法时遇到错误。 有人能告诉我上述错误的确切指针吗? 我在toggle方法中有控制器和切换方法我正在调用我使用$httpBackend模拟的HTTP PUT方法。

0 个答案:

没有答案