How can I solve this?
Hello All, i am pretty new to unit test of angular 6 using karma and jasmine
I am trying to cover a service which is inside my constructor of a component but unable to do it. The 'it' block 'should create component' covers the lines inside the constructor except for the data which is reutrned by the service .
component.ts
constructor(public firstService: FirstService, public secondService: SecondService) {
let localData :any
let id = this.secondService.getId()
let initialId = this.secondService.getInitialId()
this.firstService.getRevisions(id, initialId).subscribe((data) => {
if (data && data.content) {
localData = data.content || [];
}
});
}
component.spec.ts
describe('TestComponent', () => {
let component: TestComponent;
let fixture: ComponentFixture<TestComponent>;
let firstService: FirstService;
let secondService: SecondService;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [TestComponent],
providers: [FirstService, SecondService]
})
.compileComponents();
});
beforeEach(() => {
firstService = TestBed.get(FirstService);
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
secondService = TestBed.get(SecondService);
fixture.detectChanges();
});
it('should create Component', () => {
expect(component).toBeTruthy();
const mockData = {
"content": [
{
"name": "String",
}
],
}
spyOn(firstService, "getRevisions").and.callFake(() => {
return of({ mockData });
});
spyOn(secondService, 'getId');
spyOn(secondService, 'getInitialId');
fixture.whenStable().then(() => {
expect(firstService.getRevisions).toHaveBeenCalled();
});
});