2016-04-07 26 views
0

geçmediğine .. ve bu Bu kontrolör kodu (bir kısmını) iseİon birim test, spyOn Bu çok basit bir test

:) içine birisi biraz ışık atabilir Eğer .. geçen sözkonusu olduğuna

AppCtrl

$scope.requestAuthorization = function() { requestAuthorization(); }; 

    if ($stateParams.requestAuthorization === true) { 
     console.log('$stateParams.requestAuthorization'); 
     $scope.requestAuthorization(); 
    } 
    function requestAuthorization() { 
     console.log('requestAuthorization()'); 
     // more code here.. 
    } 

Testi

describe('AppCtrl', function() { 
    var AppCtrl, $rootScope, $scope, $stateParams; 

    beforeEach(module('myapp')); 

    // disable ionic cache to avoid GET errors 
    beforeEach(module(function($provide, $urlRouterProvider) { 
     $provide.value('$ionicTemplateCache', function() {}); 
     $urlRouterProvider.deferIntercept(); 
    })); 

    beforeEach(inject(function($controller, _$rootScope_, _$injector_, _$stateParams_) { 
     $rootScope = _$rootScope_; 
     $scope = $rootScope.$new(); 
     $stateParams = _$stateParams_; 
     AppCtrl = $controller('AppCtrl',{ 
      $scope: $scope 
     }); 
     spyOn($scope, 'requestAuthorization'); 
     $stateParams.requestAuthorization = true; 
    })); 

    it('$stateParams.requestAuthorization should be defined', function() { 
     expect($stateParams.requestAuthorization).toBeDefined(); 
    }); 

    it('$scope.requestAuthorization should be defined', function() { 
     expect($scope.requestAuthorization).toBeDefined(); 
    }); 

    // this test is not passing.. 
    it('should call requestAuthorization', function() { 
     expect($scope.requestAuthorization).toHaveBeenCalled(); 
    }); 
}); 
test edilecek ihtiyacı

Bu işlev aslında çağrılıyor, konsoldaki konsolu görebiliyorum, ancak geçmiyor.

Kolay testleri, tüm süre boyunca

Teşekkür .. sonuncusu hariç .. geçen :)

NOT: Bir $stateParams.requestAuthorization ve $scope.requestAuthorization yoktur. Birincisi boole, diğeri bir fonksiyon, fonksiyon geçmiyor.

cevap

0

ÖncedenEkran bloğunuzda, Denetleyiciyi kavramlaştırmadan önce $ stateParams öğesini tanımlayın.

beforeEach(inject(function($controller, _$rootScope_, _$injector_, _$stateParams_) { 
      $rootScope = _$rootScope_; 
      $scope = $rootScope.$new(); 
      $stateParams = _$stateParams_; 

      $stateParams.requestAuthorization = true; 

      AppCtrl = $controller('AppCtrl',{ 
       $scope: $scope, 
       $stateParams: $stateParams 
      }); 
      spyOn($scope, 'requestAuthorization'); 

     })); 
+0

Hayır, çalışmıyor: /, aslında garip, başka bir denetleyicide çok benzer bir test var ve çalışıyor .. ama bu denetleyicide değil .. – Ariel