Я только начал писать модульные тесты для своего проекта Angular, и у меня возникают некоторые проблемы с тестированием, если установлен язык.
Это код моего app.component.ts:
ngOnInit() {
this.translateService.setDefaultLang('en');
this.translateService.onLangChange.subscribe((langChangeEvent: LangChangeEvent) => {
this.localStorageService.setItem(LocalStorageService.languageKey, langChangeEvent.lang);
});
this.translateService.use('en'); <---- The subscribe callback should be called
}
Это код моего app.component.spec.ts:
describe('ngOnInit', () => {
it('should set default language as en', () => {
const translateService = fixture.debugElement.injector.get(TranslateService);
spyOn(translateService, 'setDefaultLang');
component.ngOnInit();
expect(translateService.setDefaultLang).toHaveBeenCalledWith('en');
}); <----- This part of test is successful
it('should set new lang key to localStorageService on TranslateService.onLangChange observable emit', () => {
const localStorageService = fixture.debugElement.injector.get(LocalStorageService);
spyOn(localStorageService, 'setItem');
component.ngOnInit();
translateService.use('de');
expect(localStorageService.setItem).toHaveBeenCalledWith(LocalStorageService.languageKey, 'de');
});<-------------This test throws error (see below)
});
Я получаю эту ошибку:
Я проверил в приложении, что обратный вызов подписки выполняется, однако тест говорит, что это не так.Я совершенно не понимаю всю концепцию модульного тестирования здесь?Что я делаю не так?
РЕДАКТИРОВАТЬ:
По запросу dmcgrandle - отправка полного app.component.spec.ts:
import { TestBed, async } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';
import {TranslateModule ,TranslateService, LangChangeEvent} from "@ngx-translate/core";
import {LocalStorageService} from "./core/local-storage.service";
import {OAuthService, UrlHelperService} from "angular-oauth2-oidc";
import {HttpClientModule} from "@angular/common/http";
import {ConfigurationService} from "./core/configuration.service";
import {MockConfigurationService} from './testing/mock-services/configuration.service.mock';
import { of } from 'rxjs/observable/of';
import * as fastClick from 'fastclick';
describe('AppComponent', () => {
const mockFastClick = jasmine.createSpyObj('fastClick', ['attach']);
let component : AppComponent;
let fixture;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent
],
imports: [
RouterTestingModule.withRoutes([]),
TranslateModule.forRoot(),
HttpClientModule
],
providers:[
TranslateService,
LocalStorageService,
OAuthService,
UrlHelperService,
{ provide: fastClick, useValue: mockFastClick },
{ provide: ConfigurationService, useValue: MockConfigurationService }
]
}).compileComponents();
fixture = TestBed.createComponent(AppComponent);
component = fixture.debugElement.componentInstance;
});
it('should create the app', () => {
expect(component).toBeTruthy();
});
describe('ngOnInit', () => {
it('should call fastClick.attach', () => {
spyOn(fastClick, 'attach');
component.ngOnInit();
expect(fastClick.attach).toHaveBeenCalledWith(document.body, null);
});
it('should have loaded config', () => {
fixture.detectChanges();
const appConfig = fixture.debugElement.injector.get(ConfigurationService);
expect(component.appConfig).toEqual(appConfig.config);
});
it('should set default language as en', () => {
const translateService = fixture.debugElement.injector.get(TranslateService);
spyOn(translateService, 'setDefaultLang');
component.ngOnInit();
expect(translateService.setDefaultLang).toHaveBeenCalledWith('en');
});
it('should set new lang key to localStorageService on TranslateService.onLangChange observable emit', () => {
const localStorageService = fixture.debugElement.injector.get(LocalStorageService);
spyOn(localStorageService, 'setItem');
component.ngOnInit();
expect(localStorageService.setItem).toHaveBeenCalledWith(LocalStorageService.languageKey, 'de');
});
});
});