Angular 8:如何使用Jasmine监视常量/静态属性(服务单元测试)

Angular 8:如何使用Jasmine监视常量/静态属性(服务单元测试),angular,unit-testing,mocking,Angular,Unit Testing,Mocking,我有以下服务代码 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/ht

我有以下服务代码

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);
    }
}
它总是在apiBaseUrl属性中未定义。任何快速的帮助都将不胜感激


我尝试了spyOn方法来创建模拟对象,但没有任何帮助。

以一种简单的方式离开提供者。您现在提供的是spy()而不是类

providers: [
                ApiService,
                AppConstants
            ]

取消注释const spy=spyOnProperty(AppConstants,'apiBaseUrl','get')。和.returnValue('base-api-url')这将重写just get方法。我的错误是,该属性已初始化为undefined。我将该值分配给属性,现在只需在providers数组中添加AppConstants即可。
providers: [
                ApiService,
                AppConstants
            ]