我是编程新手,这是我第一次被要求在Angular中进行单元测试,而我有点困惑... 我想在我的component.ts中测试此方法:
isInputHidden = true;
showInput(){this.isInputHidden = false;}
spec.ts:
it('should show the input', () => {
component.isInputHidden == false;
let showInput = component.showInput();
expect(showInput).toBe(true);
})
运行此测试时,出现以下错误:茉莉花==>期望undefined等于true。 在Terminal ==>中,类型'true'的参数不能分配给'Expected'类型的参数。 有人可以帮助我弄清楚我应该更改什么?
答案 0 :(得分:1)
这是修复测试的方法:
=
而不是==
初始化变量showInput()
函数更改其值后,要测试的预期变量直接为InputHidden it('should show the input', () => {
component.isInputHidden = false; // removed a "="
component.showInput(); // your function will change isInputHidden directly
expect(component.isInputHidden).toBe(true); // test isInputHidden