问题描述
我有一个简单的函数,返回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);
});