0

I am trying to run a simple unit test on angular2 app generated with angular-cli@1.0.0-beta.14 which uses ng2-translate@2.5.0.

When I run my unit tests the translate service can not load the translation file.

I believe the solution lies in a combination of karma.conf proxies property, the TranslateStaticLoader and the angular-cli.json setup.

I am using the standard cli directory structure. I have a feeling/hope this is a common problem. Will provide code and directory snippets if needed.

1 Answers1

-1

I wrote unit test with ng2-translate and I did it as the following example:

beforeEach(() => {

TestBed.configureTestingModule({
  declarations: [
    AppComponent
  ],
  providers: [
    { provide: TranslateService, useClass: TranslateServiceMock },
    { provide: TranslatePipe, useClass: TranslatePipe },
  ],

  imports: [TranslateModule]
});
  • Hi, thanks. So I did what you suggested but I can't figure out how to get mock service class to return a value. Below is my mock class. Could you show me yours? :) `class TranslateServiceMock { setDefaultLang () { } use (e) { console.log('translating', e, en); } }` – patrick.oneill Sep 22 '16 at 15:05
  • My mock look like this: import { Observable } from 'rxjs/Observable'; export class TranslateServiceMock { addLangs(langs: Array) { } setDefaultLang(lang: string) { } use(lang: string): Observable { return null; } } – user3114677 Sep 25 '16 at 07:06