2016-10-19 97 views
0

我寫了一個組件,它發佈了某些服務的數據。 我無法在單元測試中涵蓋此功能。如何輸入測試`然後'功能?如何進行單元測試。然後使用jamsine功能

angular.module('myapp') 
     .component('performAnalysis', { 
      templateUrl: 'analysis.component.html', 
      controller: PerformAnalysisController, 
      controllerAs: 'vm' 
     }); 

function PerformAnalysisController(UnitySizerService, SizingService, MarshallDTO, CommonService) { 
    let vm = this; 

    vm.$onInit = $onInit; 

    function $onInit() { 
     let unitySizerDTO = MarshallDTO.generateDTO(); 
     let previousDTO = CommonService.getProperty('previousDTO'); 

     vm.dataChanged = JSON.stringify(unitySizerDTO) === JSON.stringify(previousDTO); 

     /* Call Backend API only if DTO is changed */ 
     if (!vm.dataChanged) { 
      /* Loader On */ 
      vm.activateLoader = true; 

      SizingService.postSizingResults(unitySizerDTO).then(function (data) { 

       UnitySizerService.resultSummary = data; 
       /* New Data coming from Backend */ 
       vm.dataChanged = true; 
       /* Loader Off */ 
       vm.activateLoader = false; 
       CommonService.setProperty('previousDTO', unitySizerDTO); 
       vm.unitySizerService = UnitySizerService; 
      }); 
     } 
     else { 
      vm.unitySizerService = UnitySizerService; 
     } 
    } 
} 

這是我寫的,但我不能夠覆蓋這裏面then功能測試文件:

describe('my Component', function() { 

beforeEach(module('myApp')); 

let vm; 
let $rootScope; 
let $componentController; 
let UnitySizerService, SizingService; 

beforeEach(inject(function (_$componentController_, _$rootScope_, _UnitySizerService_, _SizingService_) { 
    $componentController = _$componentController_; 
    $rootScope = _$rootScope_; 
    UnitySizerService = _UnitySizerService_; 
    SizingService = _SizingService_; 

    vm = $componentController('performAnalysis'); 
})); 

it('should be defined', function() { 
    expect(vm).toBeDefined(); 

    expect(vm.$onInit).toBeDefined(); 

    expect(UnitySizerService).toBeDefined(); 

}); 

it('should show loader', function() { 
    vm.$onInit(); 

    vm.dataChanged = false; 

    expect(vm.activateLoader).toBeTruthy(); 
}); 

}); 
+0

是不是很簡單提取傳入'.then'的功能,只是自行測試嗎?所以你會得到像'then thenhandler(data){...}'定義的東西,它被用作'.then(thenHandler)',並且可以單獨測試'expect(thenHandler(testData),toBe(expectedResult) '等等 – vlaz

回答

1

爲了嘲笑一個無極的.then在茉莉,你可以這樣做這

var deferred = $q.defer(); 
var whatServiceReturns = "test"; 

// we are returning promise from service function 
SizingService.postSizingResults.and.returnValue({$promise:deferred.$promise}); 

// now let's call the original function 
vm.$onInit(); 

// we will resolve our promise so that we can reach inside 'then' 
// here, 'whatServiceReturns' resembles your 'data' parameter in 'then' function 
deferred.resolve(whatServiceReturns); 

$rootScope.$digest(); 

// here we can expect things like 'dataChanged' and 'activateLoader' if we see from your example 
expect(...).toEqual(...); 

您可以使用deferred.reject(someError);的錯誤。

編輯:添加註釋來闡述代碼

+0

你可以更新這個答案,我的情況是這樣的:在UT中是新的。 – ankitd

+0

@ankitd用代碼中的註釋來檢查更新的答案,詳細說明發生了什麼。 – tanmay