Mocked Http get service request for angular unit test fails because of typing

BreadcrumbPie

once more I'm trying to write a test for a http get request and it fails because of datatyping (at least that's what I suspect).

The request I want to test works and looks like that

  getTypeFilter(): Observable<Typefilter[]> {
    const url = this.endpoint.getEndpoint() + 'testdata/types?language=' + this.language;
    return new Observable<Typefilter[]>(observer => {
      this.httpc.get<any[]>(url).subscribe(
        (data) => {
          const resultData: Typefilter[] = [];
          for (const item of data) {
            resultData.push(new Typefilter(item.id, item.name));
          }
          observer.next(resultData);
          observer.complete();
        },
      );
    });
  }

excerpts from my test file look like this

import { TestBed } from '@angular/core/testing';
import { DataService } from './data.service';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { UrlService } from 'src/app/_services/url.service';
import { Typefilter } from '../_classes/typefilter';

describe('DataService', () => {
  let httpMock: HttpTestingController;
  let service: DataService;
  let urlService: UrlService;
  let mockType: Typefilter;

  mockType = {id: 1, name: 'News'};

  beforeEach(() => {

    TestBed.configureTestingModule({
      imports: [ HttpClientTestingModule]
    });
    httpMock = TestBed.inject(HttpTestingController);
    service = TestBed.inject(DataService);
    urlService = TestBed.inject(UrlService);
    urlService.getEndpoint = () => ('http://someaddress/');
  });

  it('getTypeFilter - should return TypeFilter', () => {
    service.getTypeFilter().subscribe((data) => {
      expect(data).toEqual([mockType]);
    });
    const reqMock = httpMock.expectOne((req) => req.method === 'GET' && req.url === urlService.getEndpoint() + 'testdata/types?language=' + 'de');
    expect(reqMock.request.method).toBe('GET');
    reqMock.flush(mockType);
  });
});

But it fails with 'TypeError: msgtypefilterkriterium is not iterable' - which I do understand since the data I'm recieving in my request is an array of objects and my mocked Data is only an object. But when I mock this data as an array I do get the error: 'Expected $[0] to be a kind of Object, but was Typefilter({ id: 1, name: 'News' }).' Can anyone help me out how to solve this?

satanTime

mockType is an object, whereas getTypeFilter iterates the response for (const item of data). Therefore reqMock.flush should use an array too.

Try reqMock.flush([mockType]);

about types issues, it could be fixed like that:

service.getTypeFilter().subscribe((data) => {
  expect(data).toEqual([new Typefilter(mockType.id, mockType.name)]);
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Mocking a Subject property of mocked service to be subscribed in the Angular unit test

Angular unit test fails because of component in constructor.

Unit test fails because of Sessions

Unit test component with mocked service - error

Why mocked mvc test fails with exception when service is mocked?

Angular 6 unit testing a http get request

Angular2 Service Unit Test Fails with TypeError: undefined is not an object

Angular Unit test fails when i try to inject a service

Unit test Angular service

How to get content of a request in mocked service in SoapUI?

Android unit test not mocked

Spring boot unit test not picking up request mapping for mocked controller

mockito unit test cases for Response entity performing HTTP GET request

Unexpected HTTP GET calls from within a custom Service unit test

Angular unit test with @ContentChild fails

Systemd service fails at http request

How to write unit test case for HTTPClient get() method service in Angular?

Python unit test mock, get mocked function's input arguments

Service with HTTP request in Angular

Angular unit test that calls a service

Angular Unit test for an injected service

angular service unit test DoneFn

How to test a service function in Angular that returns an $http request

How to test a http request function in a service with Angular and Jest

Angular 2 unit test http.get undefined

Moq Unit test fails when calling a Service

Unit test http context in Angular

Spring Test + Mockito.mock - Spring fails because it tries to load the mocked bean @Autowired dependencies

Validator unit test fails because of custom constraint validator