角单元测试:期望undefined等于true

时间:2020-04-16 10:50:48

标签: angular typescript unit-testing

我是编程新手,这是我第一次被要求在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'类型的参数。 有人可以帮助我弄清楚我应该更改什么?

1 个答案:

答案 0 :(得分:1)

这是修复测试的方法:

  • 使用=而不是==初始化变量
  • 直接调用该函数(由于该函数不返回任何内容,因此您无法使用它初始化showInput变量)
  • 使用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