【问题标题】:object is possibly undefined angular unit testing对象可能是未定义的角度单元测试
【发布时间】:2021-01-06 05:42:35
【问题描述】:

我正在做单元测试,但遇到错误

对象可能是“未定义”

 it('should set the dataSource filter to the provided argument', () => {
  component.applyFilter('filterValue');
   expect(this.dataSource.filter).toEqual('filterValue');
  })

it('should set the dataSource filter to the provided argument', () => {
   component.applyFilter('filterValue');
   expect(this.DMDataSource.filter).toEqual('filterValue');
 })

我在 expect(this) 中遇到错误,这里的错误是什么。

请告诉我。

【问题讨论】:

标签: angular typescript unit-testing jasmine karma-jasmine


【解决方案1】:

您应该在 expect 块内使用 component.dataSource 而不是 this.dataSource

您需要评估在component 实例中定义的dataSource

it('should set the dataSource filter to the provided argument', () => {
  component.applyFilter('filterValue');
   expect(component.dataSource.filter).toEqual('filterValue');
  })

it('should set the dataSource filter to the provided argument', () => {
   component.applyFilter('filterValue');
   expect(component.DMDataSource.filter).toEqual('filterValue');
 })

【讨论】:

    猜你喜欢
    • 2021-01-24
    • 1970-01-01
    • 2020-12-22
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    • 2020-12-01
    • 1970-01-01
    • 2021-06-06
    相关资源
    最近更新 更多