Протестируйте внедренную услугу с другой услугой в angular 5 - PullRequest
0 голосов
/ 17 июня 2020

Я пытаюсь протестировать службу, в которую внедрено другой сервис: Вот сервис, который я хочу протестировать:

@Injectable()
export class LaunchDarklyService {
    private _ldClient: LDClient;
    _flags: LDFlagSet;
    flagChange: Subject<Object> = new Subject<Object>();
    userReady: boolean;
    private ldClientIsReady:boolean

constructor(private authService: AuthService) {
    this._flags = {};

    this._ldClient = initialize(environment.launchDarklyClientId, {
        key: environment.launchDarklySDKKey,
        anonymous: true
    });

    this._ldClient.waitForInitialization().then(()=> {this.ldClientIsReady = true})

    this._ldClient.on('change', flags => {
        this.updateFlags(flags);
    });

    this._ldClient.on('ready', () => {
        this.setFlags();
    });
}

Внедренный сервис:

@Injectable()
export class AuthService {

  baseUrl = environment.authServiceBaseUrl;

  constructor(private http: HttpClient) {
  }

  logout() {
    this.removeToken();
  }

И вот мой файл .spe c .ts:

fdescribe("launchDarklyService", () => {

  let user: User;
  let mockAuthService: AuthService;
  let mockLaunchDarklyService: LaunchDarklyService;

  beforeEach(() => {
user = {
  id: "024fe324-f4c9-4b7c-a5aa-d1c67cbb6baa",
  username: "Test",
  enabled: true,
  accountNonExpired: false,
  accountNonLocked: false,
  credentialsNonExpired: false,
  authorities: [],
  createDateTime: "",
  email: "",
  firstName: "Test",
  lastName: "Test",
  salesForceUserId: "",
  updateDateTime: ""
};

mockAuthService = jasmine.createSpyObj(["me"]);

TestBed.configureTestingModule({
  providers: [
    LaunchDarklyService,
    { provide: AuthService, useValue: mockAuthService }
  ]
});

mockLaunchDarklyService = TestBed.get(LaunchDarklyService);
mockAuthService = TestBed.get(AuthService);


 });

  fit('should be created', () => {
    expect(mockLaunchDarklyService).toBeTruthy();


 });

  fit('should identify the user', () => {

    mockLaunchDarklyService.identifyUser();

    mockAuthService.me().subscribe(res => {
    });

expect(mockAuthService.me).toHaveBeenCalled();
expect(mockLaunchDarklyService.userReady).toBe(true);
  });

Но когда я запускаю тест, результаты показывают мне следующую ошибку:

ERROR: 'Cannot read property 'toPromise' of undefined'
ERROR LOG: 'Cannot read property 'toPromise' of undefined'
ERROR: 'Unhandled Promise rejection:', 'Cannot read property 'toPromise' of undefined', '; Zone:', 'ProxyZone', '; Task:', 'Promise.then', '; Value:', TypeError{}, 'TypeError: Cannot read property 'toPromise' 
of undefined
    at LaunchDarklyService.webpackJsonp.../../../../../src/api/services/launchdarkly.service.ts.LaunchDarklyService.identifyUser (http://localhost:9876/_karma_webpack_/webpack:/D:/Applications/TechInsights/CsWebClient/src/api/services/launchdarkly.service.ts:52:30)
    at LaunchDarklyService.webpackJsonp.../../../../../src/api/services/launchdarkly.service.ts.LaunchDarklyService.setFlags (http://localhost:9876/_karma_webpack_/webpack:/D:/Applications/TechInsights/CsWebClient/src/api/services/launchdarkly.service.ts:39:18)
    at http://localhost:9876/_karma_webpack_/webpack:/D:/Applications/TechInsights/CsWebClient/src/api/services/launchdarkly.service.ts:30:18
    at Object.EventEmitter.n.emit (http://localhost:9876/_karma_webpack_/webpack:/D:/Applications/TechInsights/CsWebClient/node_modules/ldclient-js/dist/ldclient.es.js:1:13666)
    at z (http://localhost:9876/_karma_webpack_/webpack:/D:/Applications/TechInsights/CsWebClient/node_modules/ldclient-js/dist/ldclient.es.js:1:26801)
    at http://localhost:9876/_karma_webpack_/webpack:/D:/Applications/TechInsights/CsWebClient/node_modules/ldclient-js/dist/ldclient.es.js:1:26552
    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/D:/Applications/TechInsights/CsWebClient/node_modules/zone.js/dist/zone.js:391:1)
    at ProxyZoneSpec.webpackJsonp.../../../../zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/webpack:/D:/Applications/TechInsights/CsWebClient/node_modules/zone.js/dist/proxy.js:129:1)
    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/D:/Applications/TechInsights/CsWebClient/node_modules/zone.js/dist/zone.js:390:1)
ERROR: 'Unhandled Promise rejection:', 'Cannot read property 'toPromise' of undefined', '; Zone:', 'ProxyZone', '; Task:', 'Promise.then', '; Value:', TypeError{}, 'TypeError: Cannot read property 'toPromise' 
of undefined

Я хотел бы знать, существует ли способ решить такой тест или мне нужно что-то изменить в службах.

...