[英]How to write angular unit test for function and switch case
extractValue(exemptionLevel: string, exemptionValue: any): string {
switch(exemptionLevel) {
case "C": return exemptionValue.isoCountryCode.toString();
case "R": return exemptionValue.districtNumber.toString();
case "D": return exemptionValue.divisionCode.toString();
case "T": return exemptionValue.terminalNumber.toString();
}
}
我在下面試過
it('expected value extractValue() c', () => {
component.extractValue('C','isoCountry');
expect('C').toEqual('isoCountry')
// expect(component.extractValue(expectedLevel[0], exemptionValue.isoCountryCode)).toHaveBeenCalled();
});
它顯示錯誤類型錯誤:無法讀取未定義的屬性“toString”
您正在向該函數發送一個字符串參數,並且您希望訪問其isoCountryCode
屬性,該屬性將始終未定義。 您必須提供帶有exemptionValue
模擬的測試函數。
it('expected value extractValue() c', () => {
const isoCountryMockResult = 'someResult';
const exemptionValue = { isoCountryCode: isoCountryMockResult } // define your mock
component.extractValue('C', exemptionValue);
expect('C').toEqual(isoCountryMockResult)
}
這應該有效,但您必須根據自己的需要進行調整
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.