对象可能是未定义的角度单位测试

时间:2020-09-19 14:53:44

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

我正在进行单元测试,但是我遇到了错误

对象可能是'undefined'

 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)里面出现错误。

请让我知道。

1 个答案:

答案 0 :(得分:1)

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

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

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');
 })