A method of a service that I am using in my component has a callback as the second argument. When This callback is executed it returns a value which is assigned to a variable in the component.
I want to mock the method in my service for unit testing and assert in the unit test that the service method was able to update the variable of my component.
export class AppComponent {
title = 'sample-app';
sampleValue: string = '';
constructor(private dataService:DataService)
{}
getSampleValue()
{
this.dataService.fetchData('Sample Value',(response) => {
this.sampleValue = response;
});
}
}
export class DataService {
constructor() {}
fetchData(inputData:string,callback:(outputData:string)=>void) {
const output:string = inputData + ' from service';
callback(output);
}
}
describe('AppComponent', () => {
let component: AppComponent;
let fixture: ComponentFixture<AppComponent>;
let mockDataService:jasmine.SpyObj<DataService>;
beforeEach(() => {
mockDataService = jasmine.createSpyObj('DataService', ['fetchData']);
TestBed.configureTestingModule({
imports: [RouterTestingModule],
declarations: [AppComponent],
providers: [{
provide: DataService,
useValue: mockDataService
}]
});
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
});
it(`should set sample value as 'Sample Value from service'`, () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
mockDataService.fetchData('Sample Value', (response) => {
app.sampleValue = response;
})
expect(app.sampleValue).toEqual('Sample Value from service');
});
});
New contributor
Amit Jindal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.