Angular-如何在jasmin中写入测试true或false-类型的错误参数不可分配给预期类型的参数<;无效>;
我有一个简单的函数返回true或false,如下所示。我得到的Angular-如何在jasmin中写入测试true或false-类型的错误参数不可分配给预期类型的参数<;无效>;,angular,unit-testing,karma-jasmine,Angular,Unit Testing,Karma Jasmine,我有一个简单的函数返回true或false,如下所示。我得到的类型为“true”的参数不可分配给类型为“Expected”的参数。我哪里出错了 app.ts onFocus(value: string) { if (value) { this.Showtimeline = true; } else { this.Showtimeline = false; } } app.spec.ts it('should return true whe
类型为“true”的参数不可分配给类型为“Expected”的参数
。我哪里出错了
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属性,以便检查该属性是否已更新
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
是布尔值,因此下面的代码基本上是将baoolean赋值给值
const value = component.Showtimeline = true;
而onFocus
希望有一个字符串类型的参数
因此,可以使用两个测试用例:
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);
});
有一些基本问题。 属性
Showtimeline
是布尔值,因此下面的代码基本上是将baoolean赋值给值
const value = component.Showtimeline = true;
而onFocus
希望有一个字符串类型的参数
因此,可以使用两个测试用例:
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);
});
谢谢你通知我。但我仍然发现'true'类型的参数不能分配给'Expected'类型的参数。应该是
expect(result).toBeTruthy()代码>?我的错误,更新上述帖子,这是因为onFocus不返回任何内容,我们必须检查Showtimeline属性onFocus()不返回任何内容,而是根据收到的“value”参数设置Showtimeline属性。所以当我们试图从这个语句中获取返回值时-constresult=component.onFocus(value);希望“结果”有价值,谢谢你通知我。但我仍然发现'true'类型的参数不能分配给'Expected'类型的参数。应该是expect(result).toBeTruthy()代码>?我的错误,更新上述帖子,这是因为onFocus不返回任何内容,我们必须检查Showtimeline属性onFocus()不返回任何内容,而是根据收到的“value”参数设置Showtimeline属性。所以当我们试图从这个语句中获取返回值时-constresult=component.onFocus(value);如果期望“结果”有一定的价值,它就会抛出错误