Tengo un componente que en sí mismo no inyecta TranslationService en el constructor. Pero uso la tubería de translate en los archivos html. La prueba siempre falla con este error:
TypeError: Cannot read properties of undefined (reading 'subscribe') at TranslatePipe.transform (http://localhost:9876/_karma_webpack_/vendor.js:107322:75) at ɵɵpipeBind1 (http://localhost:9876/_karma_webpack_/vendor.js:80764:22) at MyComponent_Template (ng:///MyComponent.js:406:33) at executeTemplate (http://localhost:9876/_karma_webpack_/vendor.js:64582:9) at refreshView (http://localhost:9876/_karma_webpack_/vendor.js:64448:13) at refreshComponent (http://localhost:9876/_karma_webpack_/vendor.js:65619:13) at refreshChildComponents (http://localhost:9876/_karma_webpack_/vendor.js:64245:9) at refreshView (http://localhost:9876/_karma_webpack_/vendor.js:64498:13) at renderComponentOrTemplate (http://localhost:9876/_karma_webpack_/vendor.js:64562:9) at tickRootContext (http://localhost:9876/_karma_webpack_/vendor.js:65793:9)Mi prueba se ve así:
describe('MyComponent', () => { let component: MyComponent; let fixture: ComponentFixture<MyComponent>; let httpClient: HttpClient; let httpTestingController: HttpTestingController; beforeEach(async () => { await TestBed.configureTestingModule({ declarations: [ MyComponent, TranslatePipe, ], imports: [ HttpClientTestingModule, RouterTestingModule, ], providers: [ { provide: TranslateService, useClass: TranslationServiceStub, } ], schemas: [NO_ERRORS_SCHEMA] }) .compileComponents(); }); beforeEach(() => { fixture = TestBed.createComponent(MyComponent); component = fixture.componentInstance; httpClient = TestBed.inject(HttpClient); httpTestingController = TestBed.inject(HttpTestingController); fixture.detectChanges(); }); it('should create', () => { expect(component).toBeTruthy(); }); });Antes de agregar TranslationServiceStub, falló debido a que faltaba TranslationService. Ahora no estoy seguro de cómo arreglar esto. ¿Algunas ideas?
Bien, como siempre, encontré la respuesta después de quejarme aquí.
Eliminé TranslationServiceStub y TranslatePipe y agregué
TranslateModule.forRoot({ loader: { provide: TranslateLoader, useClass: TranslateFakeLoader, } })a las importaciones. Ahora funciona.