(Tested with Angular 8.1.0 and ngx-translate 11.0.1)
A) If you use the translate pipe in your component, create a TranslateMockPipe
and add it to the declarations
array of your spec (as proposed in this issue).
translate-mock.pipe.ts
import {Pipe, PipeTransform} from '@angular/core';
@Pipe({
name: "translate"
})
export class TranslateMockPipe implements PipeTransform {
public name: string = "translate";
public transform(query: string, ...args: any[]): any {
return query;
}
}
your-component.spec.ts
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ YourComponent, TranslateMockPipe ],
imports: [
...
]
})
.compileComponents();
}));
In one case for some reason I also had to do step B).
B) If you use the translate service directly in your component, e.g. this.translate.get('foo.bar')
, you'll need to import the TranslateModule
and use the ngx-translate TranslateFakeLoader
as a loader
for it.
your-component.spec.ts
import {TranslateFakeLoader, TranslateLoader, TranslateModule} from '@ngx-translate/core';
...
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ YourComponent ], // you may add TranslateMockPipe from step 1 here, too
imports: [
...
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useClass: TranslateFakeLoader
}
})
]
})
.compileComponents();
}));
This way, you can use the ngx-translate built-in Stub instead of creating your own as proposed in issue (it didn't work for me either).