Skip to content

Instantly share code, notes, and snippets.

@Kamilnaja
Created March 24, 2019 12:30
Show Gist options
  • Save Kamilnaja/e9485376b6134cc544e827321974a22c to your computer and use it in GitHub Desktop.
Save Kamilnaja/e9485376b6134cc544e827321974a22c to your computer and use it in GitHub Desktop.
Angular test component with mock service, that uses http
import { async, TestBed } from '@angular/core/testing';
import { Observable, of } from 'rxjs';
import { AppComponent } from './app.component';
import { HerbsService } from './herbs.service';
import { RouterTestingModule } from '@angular/router/testing';
class MockService {
mockRespone = {
data: [
{ id: 1, name: 'hello' },
{ id: 2, name: 'world' }
]
}
getHerbs(): Observable<any> {
return of(this.mockRespone.data);
}
}
describe('AppComponent', () => {
let herbsService: HerbsService;
let component: AppComponent;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent
],
imports: [RouterTestingModule],
providers: [{ provide: HerbsService, useClass: MockService }]
});
component = TestBed.createComponent(AppComponent).componentInstance;
herbsService = TestBed.get(HerbsService)
}));
it('should create the app', async(() => {
expect(component).toBeDefined();
}));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment