Gnesh23
Gnesh23

Reputation: 39

How to mock the Observable return angular service?

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

Answers (2)

Ininiv
Ininiv

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

MoxxiManagarm
MoxxiManagarm

Reputation: 9134

Try this:

const mockResponse = {
  // ...
};

spyOn(service, 'getMydata').and.returnValue(of(mockResponse));

Upvotes: 1

Related Questions