I have a service service1
. I want to test the method getDetails()
of this. This method uses another service service2
to make an HTTP call using getData()
method and returns a promise. The function is as below:
service1 {
constructor(
private service2 : Service2,
private service3 : Service3
){}
getDetails(id: string): any{
let url = Utilty.format(id);
return service2.getData(url).pipe(map(this.service3.setData)).toPromise();
}
}
The code that I have written for testing is as below:
describe('service1', () => {
let service1: Service1;
let service2Spy: Jasmine.spyObj(Service2);
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientModule, HttpClientTestingModule],
providers: [HttpClient,
{provide: Service2, useValue: jasmine.createSpyObj('Service2', ['getData'])}
]
});
service1: TestBed.get(Service1);
service2Spy: TestBed.get(Service2);
})
it('check if getData returns correct response', function done() {
// in respnse I have taken a constant json that is expected
service2Spy.getData.and.returnValue(Promise.resolve(of(response)));
service1.getDetails('abc').then((result)=> {
expect(service2Spy.getData).toHaveBeenCalled();
done();
})
})
})
I want to test whether the below scenarios:
- Is
getData()
is called or not? - What are the parameters with which
getData
is called? - Are we getting the correct response from
getDetails
?
But I keep getting this error:
cannot read properties of undefined (reading 'pipe')