角度-如何在茉莉花中编写对或错的测试-错误类型的参数无法分配给Expected <void>类型的参数

问题描述

我有一个简单的函数,返回true或false,如下所示。我得到Argument of type 'true' is not assignable to parameter of type 'Expected<void>'。我在哪里弄错了?

app.ts

  onFocus(value: string) {
    if (value) {
      this.Showtimeline = true;
    } else {
      this.Showtimeline = false;
    }
  }

app.spec.ts

it('should return true when focus on input',() => {
    const value = component.Showtimeline = true;
    const result = component.onFocus(value);
    expect(result).toBe(true);
  });

解决方法

您需要测试功能。就像使用参数调用函数一样,更新showtimeline属性,因此您需要检查该属性是否已更新。

it('should return true when focus on input',() => {
     component.onFocus(true);        
    expect(component.Showtimeline).toBe(true);
 });

如果是假的

it('should return false when focus out input',() => {
     component.onFocus(false);        
    expect(component.Showtimeline).toBe(false);
 });
,

存在一些基本问题。 属性Showtimeline boolean ,因此基本上下面的代码将baoolean分配给 value

const value = component.Showtimeline = true;

并且onFocus期望具有 string 类型的参数。

所以两个测试用例可以是:

it('should return true when focus on input',() => {
    const value = 'true';
    component.onFocus(value);
    expect(component.Showtimeline).toBe(true);
  });

it('should return falsewhen focus on input',() => {
    const value = '';
    component.onFocus(value);
    expect(component.Showtimeline).toBe(false);
  });

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...