lundi 29 janvier 2018

irregular results while testing angular services

hey i was trying to test an angular service and i am facing this problem this is my service

    import { Ipersons } from './ipersons';
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Ialbums } from './ialbums';

@Injectable()
export class AlbumService {
  albums: any;
  persons: any;
  constructor(private http: HttpClient) {
   }
   getImages(start, limit)
   {
     const url = 'https://jsonplaceholder.typicode.com/photos?_start='+start+'&_limit='+limit;
      return this.http.get<Ialbums[]>(url);
   }
   getPersons(id)
   {
     const url= 'https://jsonplaceholder.typicode.com/users/'+id;
     return this.http.get<Ipersons[]>(url);
   }
}

then this is my spec.ts

    import { HttpClient } from '@angular/common/http';
import { TestBed, inject } from '@angular/core/testing';
import { Ialbums } from './ialbums';
import { AlbumService } from './album.service';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
describe('AlbumService', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [
        HttpClientModule
      ],
      providers: [AlbumService, HttpClient]
    });
  });

  it('should be created', inject([AlbumService], (service: AlbumService) => {
    expect(service).toBeTruthy();
  }));
  fit('should return json array', inject([AlbumService], (service: AlbumService) => {
    service.getImages(1, 1).subscribe(result => {
      expect(result[0].id).toBe(4);
     });
  }));
});

when i am running the test it returns successful even when i actually am giving the wrong value to compare for example result[0].id value is 2 but it returns success even when i give it to be 4

Aucun commentaire:

Enregistrer un commentaire