1 回答

TA貢獻1826條經驗 獲得超6個贊
作為最佳實踐的一部分,我建議進行一些更改。
不要模擬組件方法,因為您需要測試它們。在您的情況下,您應該設置 value?
this.service.Type.description
,因此它應該返回true
or?false
。這將是一個正確的做法。
如果this.service
是已注入的服務construtor
,則可以模擬該服務。
由于您正在使用 測試多個條件
if
?else
,因此您需要編寫幾個it
塊才能獲得良好的測試覆蓋率。要進行測試
var r
,您應該將其聲明public
為組件級別的變量,而不是在function
.?let
也更喜歡var
.
這是一個示例代碼,您可以編寫該代碼來設置其中的值isBrilliant()
it('should push Brilliant when the Description is so,()=>{
? ?component.service.Type.description = 'Brilliant';
? ?component.onExportToExcel();
? ?expect(component.r.length).toBe(1);
? ?expect(component.r[0]).toBe('This is Brilliant');
})
it('should push other cool content when the Description is not Brilliant,()=>{
? ?component.service.Type.description = 'something else';
? ?component.onExportToExcel();
? ?expect(component.r.length).toBe(2);
? ?// check other values in expect block accordingly
})
// you should also check that "component.r" has zero length when hasDescription() returns false
我希望上面的代碼片段能給你一個好的開始
添加回答
舉報