Reputation: 33
I have the following service code
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { AppConstants } from 'src/app/constants';
import * as _ from 'underscore';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class ApiService {
constructor(private httpClient: HttpClient) { }
get(url: string, options?: any): Observable<any> {
// console.log('2. AppConstants', AppConstants.apiBaseUrl);
const requestURL = `${AppConstants.apiBaseUrl}${url}`;
console.log('url---', requestURL);
return this.httpClient.get(requestURL, options).pipe(
catchError(this.handleError)
);
}
post(url: string, body: any, options?: any): Observable<any> {
return this.httpClient.post(`${AppConstants.apiBaseUrl}${url}`, body, options).pipe(
catchError(this.handleError)
);
}
put(url: string, body: any, options?: any): Observable<any> {
return this.httpClient.put(`${AppConstants.apiBaseUrl}${url}`, body, options).pipe(
catchError(this.handleError)
);
}
delete(url: string, options?: any): Observable<any> {
return this.httpClient.delete(`${AppConstants.apiBaseUrl}${url}`, options).pipe(
catchError(this.handleError)
);
}
private handleError(error: HttpErrorResponse) {
if (_.isUndefined(error) && _.isNull(error)) {
throwError(error);
}
if (error.error instanceof ErrorEvent) {
// A client-side or network error occurred. Handle it accordingly.
console.error('An error occurred:', error.error.message);
} else {
// The backend returned an unsuccessful response code.
// The response body may contain clues as to what went wrong,
console.error(
`Api returned an error with code ${error.status}, ` +
`error body was: ${error.error}`);
}
return throwError(error.error);
}
}
Note: This service uses a file that has specific static conntant variables.
I am trying to mock the AppConstants.apiBaseUrl variable in the .spec file as below
import { AppConstants } from 'src/app/constants';
import { HttpTestingController, HttpClientTestingModule } from '@angular/common/http/testing';
import { async, inject, TestBed } from '@angular/core/testing';
import { ApiService } from './api.service';
class MockAppConstants {
public static apiBaseUrl = 'test-base-api-url';
}
describe('ApiService', () => {
// const spy = spyOnProperty(AppConstants, 'apiBaseUrl', 'get').and.returnValue('base-api-url');
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule
],
providers: [
ApiService
,
{
provide: AppConstants,
useValue: spy
}
]
});
}
);
it('should get profile data of user', () => {
const profileInfo = { login: 'blacksonic', id: 602571, name: 'Gábor Soós' };
const githubService = TestBed.get(ApiService);
const appConstants = TestBed.get(AppConstants);
const http = TestBed.get(HttpTestingController);
let profileResponse;
githubService.get('blacksonic').subscribe((response) => {
profileResponse = response;
});
http.expectOne('undefinedblacksonic').flush(profileInfo);
expect(profileResponse).toEqual(profileInfo);
});
It always getting undefined in the apiBaseUrl property. Any quick help will be much appreciated.
I have tried the spyOn method to create mock objects but it didn't help.
Upvotes: 1
Views: 5909
Reputation: 12196
leave providers in a simple way. you are providing a spy() now instead of the class.
providers: [
ApiService,
AppConstants
]
Upvotes: 2