簡體   English   中英

如何在角度2單元測試中訪問方法的局部變量

[英]How to access local variable of method in angular 2 unit test

我正在嘗試用打字稿在angular 2中編寫方法的單元測試用例。 在那我有一個局部變量,它決定將調用哪個函數。

getData(){
    let condition = route.queryParam.refer;
    if(condition === 'productWithChild'){
         this.LoadProductWithChildData(); 
    } else if(condition === 'product') {
         this.LoadProduct();
    }else{
        this.showMessage('No Data is Available');
    }
}

請建議如何為此方法編寫單元測試用例。

 // Code to test: getData(){ let condition = route.queryParam.refer; if(condition === 'productWithChild'){ this.LoadProductWithChildData(); } else if(condition === 'product') { this.LoadProduct(); }else{ this.showMessage('No Data is Available'); } } // Sample code, but not complete: describe('getData()', () => { it('should call the LoadProductWithChildData() function when the route.queryParam.refer value is "productWithChild"', () => { // Mock your route.queryParam.refer value here. component.route = { queryParam: { refer: 'productWithChild' } }; spyOn(component, 'LoadProductWithChildData'); expect(component.LoadProductWithChildData) .toHaveBeenCalled(); }); // Repeat the above for each of the conditions you have in your if()else() block. }); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM