12. Test EventService
In this section we will fix the now broken spec files missing newly injected dependencies in the components and services under test.
1. Fix broken tests with injected dependencies
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { EventComponent } from './event.component';
import { NO_ERRORS_SCHEMA } from '@angular/compiler/src/core';
import { HttpClientModule, HttpClient } from '@angular/common/http';
import { EventService } from '../../services/event.service';
describe('EventComponent', () => {
let component: EventComponent;
let fixture: ComponentFixture<EventComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
providers: [
{ provide: HttpClient, useValue: null },
{
provide: EventService,
useValue: {
getAttendees: () => {}
}
}
],
declarations: [EventComponent],
schemas: [NO_ERRORS_SCHEMA]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(EventComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});2. Fix EventService tests
3. Add a spy to pass in fake attendees to mock the service
StackBlitz Link
Extras and Homework
Add EventService tests
Last updated