P
P
Programep2021-12-06 22:16:59
Angular
Programep, 2021-12-06 22:16:59

How to call mock service method in jasmine unit tests?

I'm trying to make a test using a mock service method call that is used in a real application to emulate API requests. In order not to duplicate, I wanted to use the same method in a unit test, but it looks like apiServiceMock.getPages(queryModel) returns undefined because when the test is executed in VS Code Test Explorer, the error TypeError is displayed: Cannot read properties of undefined (reading 'request')
If you copy to the result variable with test values ​​from the mock service, then the test works without errors.
Please tell me how best to make this test and whether it is possible to reuse the mock service method so as not to duplicate the test data.
Test code:

it('#return correct response', () => {
    const queryModel = new QueryModel();
    const result: Observable<PaginationModel> = apiServiceMock.getPages(queryModel);

      realService.getPages(queryModel).subscribe(response => {
        expect(response).toEqual(result);
      });

    const req = httpMock.match(`${test_url}`)[0];
    expect(req.request.method).toBe("GET");
    req.flush(result);
  });

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question