I have a simple test that is structured like this:
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { BreadcrumbsComponent } from './breadcrumbs.component';
import { RouterTestingModule } from '@angular/router/testing';
import { TranslateLoader, TranslateModule, TranslateService } from '@ngx-translate/core';
import { HttpClient } from '@angular/common/http';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { BreadcrumbsService } from './breadcrumbs.service';
import { multiTranslateHttpLoaderFactory } from 'app/app.component';
fdescribe('BreadcrumbsComponent', () => {
let component: BreadcrumbsComponent;
let translateService: TranslateService;
let fixture: ComponentFixture<BreadcrumbsComponent>;
let translateServiceStub: Partial<TranslateService>;
translateServiceStub = {
getBrowserLang() {
return 'de';
},
};
beforeEach((() => {
TestBed.configureTestingModule({
declarations: [BreadcrumbsComponent],
imports: [
RouterTestingModule,
HttpClientTestingModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: multiTranslateHttpLoaderFactory,
deps: [HttpClient]
}
}),
],
providers: [
{provide: TranslateService, useValue: translateServiceStub},
BreadcrumbsService
]
});
translateService = TestBed.get(TranslateService);
}));
beforeEach(() => {
fixture = TestBed.createComponent(BreadcrumbsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
Currently this error is thrown, which I just can not handle:
TypeError: Cannot read property 'subscribe' of undefined
This is my class and the one line in the ngOnInit method causes this error:
export class BreadcrumbsComponent implements OnInit {
breadcrumbs: Breadcrumb[] = [];
constructor(private router: Router,
private activeRoute: ActivatedRoute,
private translate: TranslateService,
private breadcrumbsService: BreadcrumbsService) {
}
ngOnInit() {
this.translate.onLangChange.subscribe(() => this.identifyRoute());
}
...
What am I doing wrong here? Or. Is there a way to get around the error or to mock this line so that the test goes through? I really have no idea how to mock this line in my test.
Aucun commentaire:
Enregistrer un commentaire