Reputation: 39
this.variable = this.service.getMydata();
The above service is returning Observable and for the variable I have subscribed as below.
this.variable.subscribe(data => { My logic here});
How would right unit test for this in angular 6?
Upvotes: 0
Views: 131
Reputation: 1325
Mock the service and the method to return observable
const mockService = {
getMydata: {
return of(<Response>)
}
}
TestBed.configureTestingModule({
declarations: [...],
providers: [{provide: Service, useValue: mockService}]
})
Upvotes: 0
Reputation: 9134
Try this:
const mockResponse = {
// ...
};
spyOn(service, 'getMydata').and.returnValue(of(mockResponse));
Upvotes: 1