Writing an angular2 test for a service with constructor parameters

How can I test a service that uses constructor parameters? I am trying to learn about testing with docs , but I am having problems. I have a service location-search.service.ts:

import { Injectable }       from '@angular/core';
import { Http, Response, Jsonp }   from '@angular/http';
import { Observable }       from 'rxjs';

import { Location }         from '../models/location';

@Injectable()
export class LocationSearchService {
    constructor(private jsonp: Jsonp) {}

    search(term: string): Observable<Location[]> {
        return this.jsonp
            .get(/* api url */)
            .map((r: Response) => r.json().RESULTS as Location[]);
    }
}

Now that I am writing location-search.spec.ts, how can I enter jsonpthe service correctly ? eg.

describe('LocationSearchService', () => {
  let service: LocationSearchService;

  // how do I initialize LocationSearchService with Jsonp in this case?
  beforeEach(() => { service = new LocationSearchService(); });

  it('#search should return observable value', () => {
    expect(service.search('term').toBe('observable value');
    done();
  });
});
+4
source share

Source: https://habr.com/ru/post/1661392/


All Articles