The pipe 'translate' could not be found , angular2 component testing

◇◆丶佛笑我妖孽 提交于 2019-12-01 02:51:10

it's the ng2-translate github.com/ocombe/ng2-translate

You need to configure the TestBed with the library module just like you would configure the library with your real application. And looking at the documentation, it shows configuring it by importing the module

imports: [
  TranslateModule.forRoot()
]

So you should do the same in the TestBed configuration

TestBed.configureTestingModule({
  declarations: [ RightComponent ],
  imports: [TranslateModule.forRoot()]
});

This is what the TestBed.configureTestingModule is for: to configure a module for the test environment.

With latest Angular 4 compatible ngx-translate you need to implement this directly into the component you want to test:

import {TranslateHttpLoader} from "@ngx-translate/http-loader";
import {Http, HttpModule} from "@angular/http";
import {
    MissingTranslationHandler,
    TranslateLoader,
    TranslateModule,
    TranslateService
} from "@ngx-translate/core";

...

export function HttpLoaderFactory(http: Http) {
    return new TranslateHttpLoader(http, "./assets/i18n/", ".json");
}

   ...

   imports: [
                TranslateModule.forRoot({
                    loader: {
                        provide: TranslateLoader,
                        useFactory: HttpLoaderFactory,
                        deps: [Http]
                    }
                })
            ],
   ...

   providers: [
                TranslateService
   ...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!