Angular Extended Service Test

5 views
Skip to first unread message

ALBy

unread,
May 5, 2020, 11:50:44 PM5/5/20
to Angular and AngularJS discussion
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.  
Reply all
Reply to author
Forward
0 new messages