【关键词】
单元测试框架、HarmonyOS Test、assertThrowError、assertFail、assertEqual
【测试代码及测试结果展示】
这里以新建API9工程自动生成的ohosTest来编写单元测试代码。
1、 测试代码:
import { describe, it, expect } from '@ohos/hypium'
import abilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry';
class CharacterReader {
constructor() {
}
unconsume(){
throw new Error("this is an error")
}
unconsume1(a: number,b: number){
if (a == 1 && b == 2) {
throw new Error("this is an error")
}
}
add(a: number,b: number) {
return a + b
}
}
export default function abilityTest() {
describe('ActsAbilityTest', function () {
// 验证待验证方法的行为时,不带括号
it('assertThrowError',0,() => {
let reader = new CharacterReader()
expect(reader.unconsume).assertThrowError("this is an error")
})