2017-12-19 15 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 –

+0

1つの提案:もうangularJsの書き込みテストをいけません。あなたがテストで興味のあるポイントではないすべてのdaの事をモックする以外は、角度2+のアップグレードの可能性はなく、テストできるものは非常に限られています。 – CodeNashor

答えて

関連する問題