data.json() is not a function error in Testing

2019-09-20 06:28发布

问题:

I am doing integration testing of a component.

public ngOnInit() {

    this.employeeService.getEmployees()
        .subscribe(data => {
            this.employees.next(data.json());
    });

  }

The above code is working fine when serving the project.

But during testing, it is throwing a error saying data.json() is not a function.

回答1:

You need to mock EmployeeService's getEmployees() method and return same response type object as the original.