无法手动触发matSortChange事件,获取错误指令未定义

时间:2019-04-27 22:06:23

标签: angular unit-testing karma-jasmine

我正在用角度应用程序运行单元测试。我想为matsortchange事件手动触发matSort指令。此外,我尝试将属性值(如matsortdirection)从asc更改为desc等,或者在我尝试打印到控制台的click / matsortevent上。我无法做到这一点

template.html
<table id=tabmat_1 matSort (matSortChange)="sortData(i, $event)" matSortActive="enddate" matSortDirection="desc" matSortDisableClear >

@Directive({

    selector:'matSort',
    host:{'(matSortChange)':'onClick()'}
})
export class DemoClass{

  matSortActive="startdate";
  matSortDirection="asc";
  onClick(){
    console.log('matsort trigger worked!!!');
  }
}

  let matsort1=fixture.debugElement.queryAll(By.directive(DemoClass))
  let matsortdirective=matsort1[0].injector.get(DemoClass);
  matsort1[0].triggerEventHandler('matSortChange',null);

  console.log(matsort1)//I want to print here, matsort trigger worked!!!
  })  

0 个答案:

没有答案