I have a an Angular app with a service file that constructs the URL for API calls and unpacks the returned JSON. All of the methods in the file are variations on this theme:
getExampleReport(skip: number, take: number, gridSort: SortDescriptor[]) {
let queryString = '?Skip=' + skip + '&Take=' + take;
if(gridSort && gridSort.length > 0 && gridSort[0].dir && gridSort[0].field) {
queryString = queryString + '&SortField=' + gridSort[0].field + '&SortDirection=' + gridSort[0].dir;
}
return this.httpClient
.get<ExampleReportDisplayModel>(
this.globals.ApiUrl + '/Path/To/API/ExampleReport' + queryString
)
.pipe(
map(data => {
if (data !== null) {
return this.ExampleReportMapper.mapToModel(data);
}
})
);
}
I have a corresponding .spec.ts file where all of those methods are tested.
it('getExampleReport should return data', () => {
spyOnProperty(globalService, 'ApiUrl').and.returnValue('http://testUrl');
const responseData = {
totalCount: 1,
reportItems: [
{
itemNumber: 1,
itemStatus: 'TEST',
accountNumber: '111111111',
state: 'AA',
customerName: 'Test McTest',
processorName: 'Test MacTest'
}]
};
var sort: SortDescriptor[] = [
{
dir: "asc",
field: "itemStatus"
}
];
service.getExampleReport(0, 1, sort).subscribe(
(data) => {
expect(data.totalCount).toBe(1);
}
);
const httpRequest = httpMock.expectOne('http://testUrl/Path/To/APIExampleReport?Skip=0&Take=1&SortField=itemStatus&SortDirection=asc');
expect(httpRequest.request.method).toBe('GET');
httpRequest.flush(responseData);
httpMock.verify();
});
Some of these tests work as I expect, but several of them are persistently giving me this error message.
1) getExampleReport should return data
ReportsService
Error: Expected one matching request for criteria "Match URL: http://testUrl/Path/To/APIExampleReport?Skip=0&Take=1&SortField=itemStatus&SortDirection=asc", found none.
at HttpClientTestingBackend.expectOne (http://localhost:9876/_karma_webpack_/node_modules/@angular/common/fesm2015/http/testing.js:487:1)
at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/src/app/workflows/example_workflow/services/reports.service.spec.ts:122:34)
at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:359:1)
at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:308
at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:358:1)
at Zone.run (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:124:1)
at runInTestZone (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:561:1)
at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:576:1)
at <Jasmine>
Error: Expected no open requests, found 1: GET http://testUrl/Path/To/APIExampleReport
at HttpClientTestingBackend.verify (http://localhost:9876/_karma_webpack_/node_modules/@angular/common/fesm2015/http/testing.js:538:1)
at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/src/app/workflows/example_workflow/services/reports.service.spec.ts:29:14)
at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:359:1)
at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:308
at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:358:1)
at Zone.run (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:124:1)
at runInTestZone (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:561:1)
at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:576:1)
This error confuses me. I see that it isn't seeing the GET request expected by httpMock.expectOne(...);
and that it is seeing a response when it expects none from httpMock.verify();
. I added logging to the service method and confirmed that the method is being called and that queryString
is building the exact string I expect. Even more curious, these tests are passing on a remote build server, but some of them (not all!) of them fail for me locally.
Why is my test not passing?