I'm struggling with testing an Angular2 component that has a service injected. The test code is below but basically:
• SearchComponent takes a FightService in the constrctor.
• The constructor calls a flightsService.getFlights() which fires off an HTTP request. flightsService.getFlights() returns an observable.
• The constructor subscribes to the observable returned which populates the allSummaryItems array.
My MockFlightService isn't being used, it basically fails saying there is no provider for Http (which is in the FlightService constructor). If I add HttpModule to the providers in TestBed, then it goes off and fires the real Http request.
How can I make sure I'm using MockFlightService? Also will this test the observable properly, even when firing a real Http request I can see that the subscribed methods aren't being called?
Thanks
class MockFlightsService {
public getFlights = () => {
return new Observable<any>(() => { return dummyData.json(); });
};
}
describe('SearchComponent Tests', () => {
let fixture: ComponentFixture<SearchComponent>;
let component: SearchComponent;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [SearchComponent],
imports: [RouterModule],
providers: [{ provide: FlightsService, useClass: MockFlightsService }]
});
fixture = TestBed.createComponent(SearchComponent);
fixture.detectChanges();
});
it('should render list', fakeAsync(() => {
fixture.whenStable();
component = fixture.componentInstance;
console.log(component.allSummaryItems); // this is empty, shouldn't be
}));
});
I'm using Angular 2.0.1.