dimanche 12 août 2018

Angular test Argument of type is not assignable

I'm trying to write a test for a service which retrieves data from server. I have following Site model

import { Feature } from './feature.model';
import { Deserializable } from './deserializable.model';

export class Site implements Deserializable {
  path: string;
  siteLink: string;
  features: Feature[];

  deserialize(input: any): this {
    Object.assign(this, input);
    this.features = input.map(feature => new Feature().deserialize(feature));
    return this;
  }
}

And service

API_URL = '/sites';

  constructor(
    private http: HttpClient
  ) { }

  getSites(): Observable<Site[]> {
    return this.http.get<Site[]>(this.API_URL).pipe(
      map((response: Site) => new Site().deserialize(response))
    );
  }

When I run my test it says

ERROR in src/app/Services/site.service.spec.ts(54,31): error TS2345: Argument of type '{ path: string; siteLink: string; features: string[]; }[]' is not assignable to parameter of type 'Any | Spy | ArrayLike | ObjectContaining> | ArrayContaining'. Type '{ path: string; siteLink: string; features: string[]; }[]' is not assignable to type 'ArrayContaining'. Property 'asymmetricMatch' is missing in type '{ path: string; siteLink: string; features: string[]; }[]'.

And there is my test

describe('SiteService', () => {
  let injector: TestBed;
  let service: SiteService;
  let httpMock: HttpTestingController;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule],
      providers: [SiteService]
    });
    injector = getTestBed();
    service = injector.get(SiteService);
    httpMock = injector.get(HttpTestingController);
  });

  afterEach(() => {
    httpMock.verify();
  });

  it('should be created', inject([SiteService], (serviceC: SiteService) => {
    expect(serviceC).toBeTruthy();
  }));

  describe('#getSites', () => {
    it('should return an Observable<Site[]>', () => {
      const dummySites = [
        {
          path: 'https://example.com/img1.img',
          siteLink: 'http://example1.com',
          features: [
            'feature 1',
            'feature 2',
            'feature 3'
          ]
        },
        {
          path: 'https://example.com/img2.img',
          siteLink: 'http://example2.com',
          features: [
            'feature 1',
            'feature 2',
            'feature 3'
          ]
        }
      ];

      service.getSites().subscribe(sites => {
        expect(sites.length).toBe(2);
        expect(sites).toEqual(dummySites);
      });

      const req = httpMock.expectOne(`${service.API_URL}`);
      expect(req.request.method).toBe('GET');
      req.flush(dummySites);
    });
  });
});

As for me it looks equal (also didn't find any "deepEqual")

1 commentaire: