Hello, I am testing my app with Jasmine and Karma, but I am having some problems with my service. The code below is my service;
export class CustomerService extends SMService{
...
getCustomers(sort: string,
order: string,
page: number,
filter: CustomerFilter): Observable<SMServiceListResponse<Customer>> {
return super.getObjects(sort, order, page, filter);
}
...
}
I want to test if method 'getObjects' is called when getCustomers is executed. Below is the it() :
let mocker: Mocker;
let mockCustomer: any;
beforeEach(() => {
mocker = new Mocker();
mockCustomer = {data: mocker.getCustomers(2), pagination: {}};
});
afterEach(() => {
httpTestingController.verify();
});
it('should return expected customers by calling once', () => {
spyOn(customerService, 'getObjects').and.callThrough();
customerService.getCustomers('', '', 0, null).subscribe(
response => {
expect(response).toEqual(mockCustomer, 'should return expected customers');
expect(response.data.length).toEqual(mockCustomer.data.length);
}
);
// Error: This is never called
expect(customerService.getObjects).toHaveBeenCalledTimes(1);
const req = httpTestingController.expectOne(customerEndpoint);
expect(req.request.method).toEqual('GET');
req.flush(mockCustomer); // Return expected Customers
});
Everything is ok but .getObjects is never called. I have tried a lot of ways to solve this but cant do it.