2017-12-19 5 views
0

저는 AngularJS 앱에서 단위 테스트를 위해 Jasmine과 Karma를 사용하고 있습니다. 컨트롤러 기능에서 내부 변수의 값을 확인해야합니다.재스민을 사용하여 함수의 내부 변수를 확인하십시오.

여기 내 컨트롤러입니다.

(function() { 
    'use strict'; 
    angular 
     .module('testApp',[]) 
     .controller('testController', testController); 

    function testController($http, $scope, $timeout) { 
     var vm = this; 
     vm.getTestValues = getTestValues; 

     function getTestValues(){ 

      vm.serverError = undefined; 
      vm.priceList = []; 

      $http.get('https://example.com/api/getPrice').then(function(response) { 

       vm.priceList = response.data; 

       vm.value = vm.priceList[0].itemValue; 

       vm.totalValue = vm.value * 10;    

      }).catch(function(e){ 
       vm.serverError = 'Server Error'; 
      }); 
     } 
    } 
})(); 

여기 내 테스트 코드

describe('Test Controller', function() { 
    beforeEach(module('testApp')); 

    var ctrl; 

    beforeEach(inject(function($rootScope, $controller){ 

    scope = $rootScope.$new(); 
    ctrl = $controller('testController', { $scope: scope }); 
    })); 


    describe('vm.value', function() { 
    it('Should be ', function() {  

     ctrl.getTestValues(); 

     console.log(ctrl.priceList); 


    }); 
    }); 

}); 

console.log(ctrl.priceList); 인쇄 []입니다.

어떻게 vm.priceList, vm.valuevm.totalValue의 값에 액세스 할 수 있습니까?

+1

https://docs.angularjs.org/api/ngMock/service/$httpBackend https://docs.angularjs.org/api/ngMock/service/$httpBackend –

+0

한 가지 제안 : 더 이상 AngularJS와 대한 해달라고 쓰기를 테스트합니다. 각도 2+에 대한 업그레이드 가능성은 없으며 테스트 할 수있는 것들은 매우 제한적입니다. 단, 테스트에서 중요한 것은 아닙니다. – CodeNashor

답변

0

$http.get은 비동기 호출입니다. HTTP 서비스를 테스트 할 때는 $httpBackend을 사용해야합니다.

다음 링크를 통해 더 자세히 살펴볼 수 있습니다.

how to test $http call in angular js?

관련 문제