2016-03-15 170 views
0

我想測試我的控制器,但運行因緣時,我得到了一個未定義開始茉莉花測試angularjs控制器

這是我的控制器代碼:

(function() { 
    'use strict'; 

    angular 
     .module('tariffOptionsExtras') 
     .controller('tariffOptionsExtrasBookCtrl', tariffOptionsExtrasBookCtrl); 

    tariffOptionsExtrasBookCtrl.$inject = ['tariffOptionsExtrasSrv', '$location', 'cache', 'authSrv', '$stateParams', 
     'tariffOptionsExtrasBookSrv', 'minimumDuration', '$rootScope', 'userTypes', 'messageCtrl', '$state']; 

    function tariffOptionsExtrasBookCtrl(tariffOptionsExtrasSrv, $location, cache, authSrv, stateParams, tariffOptionsExtrasBookSrv, minimumDuration, $rootScope, userTypes, messageCtrl, $state) { 
     var vm = this; 
     var cacheName = 'tariffOptionsExtras'; 

     var cacheCollection = cache.getCollection(cacheName); 
     vm.date = new Date(); 
     vm.minimumDuration = minimumDuration; 
     //if statement commented out. prepaid users should be able to enter the flow. 
Date(vm.bookOption.startDate) 
     } 
     if (!vm.bookOption) { 
      $state.go(pagesConfig.tariffOptionsExtras.name); 

     } else { 
      vm.infoLink = vm.bookOption.infoUrl; 
     } 

     /** 
     * Runs booking tarif extra post and in case of success the view is changed 
     */ 
     vm.book = function() { 
      //If bookoption not present, redirect to chooser 
      tariffOptionsExtrasBookSrv.bookTariffExtra(vm.bookOption).then(function(response) { 
       $rootScope.$broadcast('transactionFinished'); 
       var item = response['salesOrderVBO'][0]['orderItems']['orderItem'][0]; 
       if (item.product.action == 'BOOKED') { 
        vm.success = true; 
       } 
      }, function(reason) { 
       vm.errorMessage = reason.data.faultMessage; 
      }); 
     }; 
     vm.success = false; 
     vm.subscription = authSrv.getUserContract(); 
     vm.msisdn = vm.subscription.subscription.msisdn; 

    } 
})(); 

單元測試茉莉花

describe('tariffOptionsExtras module', function() { 


    describe('tariffOptionsExtrasBook Controller', function() { 
     var tariffOptionsExtrasBookCtrl, tariffOptionsExtrasSrv, tariffOptionsExtrasBookSrv, authSrv; 
     // Step 1: Import the module this controller belongs to, and its dependencies 
     beforeEach(function() { 
      module('app.common'); 
      module('tariffOptionsExtras'); 
     }); 

     // Step 2: Mock any service that initially used when the controller instantiate 
     // beforeEach(module(function($provide) { 

     //  $provide.factory('tariffOptionsExtrasBookSrv', function() { 
     //   var getSync; 
     //   getBookedExtras = function() { 
     //    return { 
     //     then:function(){} 
     //    }; 
     //   }; 
     //   getTariffBookableOptions = function() { 
     //    return { 
     //     then:function(){} 
     //    }; 
     //   }; 
     //   return { 
     //    getBookedExtras: getBookedExtras, 
     //    getTariffBookableOptions:getTariffBookableOptions 
     //   }; 
     //  }); 
     // })); 
     beforeEach(module(function($provide) { 
      $provide.factory('authSrv', function() { 
       getUserContract = function() { 
        return { 
         subscription:{ 
          msisdn:'491741660390', 
          mboName:'27434975' 
         }, 
         contract:{ 
          mboName:'27434975', 
          ban:'106491816', 
         } 
        }; 
       }; 
       return { 
        getUserContract: getUserContract, 
       }; 
      }); 

     })); 


     // Step 3: Inject $controller with its dependencies 
     beforeEach(function() { 
      // 1. Import the module 
      module('app'); 
      // 2. Inject $controller 
      inject(function($controller, $rootScope, _authSrv_, _tariffOptionsExtrasSrv_, _tariffOptionsExtrasBookSrv_) { 
       authSrv = _authSrv_; 
       tariffOptionsExtrasSrv = _tariffOptionsExtrasSrv_; 
       tariffOptionsExtrasBookSrv = _tariffOptionsExtrasBookSrv_; 

       // 3. Use $controller to instantiate the controller 
       tariffOptionsExtrasBookCtrl = $controller('tariffOptionsExtrasBookCtrl', { 
        'authSrv': authSrv, 
        'tariffOptionsExtrasSrv': tariffOptionsExtrasSrv, 
        'tariffOptionsExtrasBookSrv': tariffOptionsExtrasBookSrv 
       }); 
      }); 
     }); 


     // Step 4: Test the controller 
     it('Should return sum of 1+1', function() { 
      expect(1+1).toBe(2); 
     }); 

    }); 
}); 

當運行業力開始我得到這個屏幕: enter image description here

此外,當我評論的代碼塊它的工作原理:

tariffOptionsExtrasBookCtrl = $controller('tariffOptionsExtrasBookCtrl', { 
       'authSrv': authSrv, 
       'tariffOptionsExtrasSrv': tariffOptionsExtrasSrv, 
       'tariffOptionsExtrasBookSrv': tariffOptionsExtrasBookSrv 
      }); 
+0

請測試:'希望(tariffOptionsExtrasBookCtrl).toBeDefined();',如果它是不確定的,你知道的貢獻做什麼呢:) – Jagrut

+0

謝謝Jagrut這個很多幫助我:)) –

回答

0

首先你要注入$控制器 PARAM必須用下劃線封閉 然後將其分配到$控制變量如本例

beforeEach(inject(function ($rootScope, _$controller_) { 
scope = $rootScope.$new(); 
$controller = _$controller_; 
$controller('nameController', {$scope: scope}); 
})); 
+0

感謝塞薩爾:) ) –