如何确保获取的数据正确

时间:2019-04-12 15:08:00

标签: angular unit-testing

我正在按角度运行单元测试,我想确保获取的数据充满了所应用的过滤器/条件。我已经在代码中清楚地解释了

这是要测试的方法

getData() {

    this.http.post<Transactions[]>('/api/getrecords', this.filter)
    .subscribe(res => {
      if (res['status'] == "FAILURE") {
        console.log(res['err']);

      } else {
        console.log(res['status']);
        this.data = res['data'];
       }
}

这是过滤器

filter = {
    from : "",
    to : "",
  };

这是我的测试,当我在getData中打印数据违抗的值时,它是未定义的

component.filter.from="2019-03-10T22:00:00.000Z";//10th march
component.filter.to="2019-04-11T21:59:59.000Z";//11th april
let getDataSpy = spyOn(component, "getData").and.callThrough();
mockService.getData=of(mockUsageRecords);

component.getData();
fixture.detectChanges();

console.log(component.data);//I expect this to be populated with the fetched data from http.post, but this is empty array. I want the filtered data to appear here

0 个答案:

没有答案