-
-
Notifications
You must be signed in to change notification settings - Fork 584
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
unit testing with ng2-translate #235
Comments
Sorry, I did not mention when is failing. This is the body of my AppComponent: import { Component, ViewContainerRef } from '@angular/core';
import { TranslateService } from 'ng2-translate/ng2-translate';
import { DEFAULT_LANGUAGE, LANGUAGE_STORAGE_KEY, isSupportedLanguage } from './common';
@Component({
selector: 'lp3-app',
styles: [require('./app.component.scss')],
template: require('./app.component.html')
})
export class AppComponent {
constructor(private translate: TranslateService, private viewContainerRef: ViewContainerRef) {
console.debug('AppComponent activated');
var userLang = localStorage.getItem(LANGUAGE_STORAGE_KEY);
if (userLang) {
console.debug('Restoring language from localStorage');
} else {
console.debug('No stored language!');
userLang = navigator.language.split('-')[0]; // use navigator lang if available
console.debug('Browser detected language is', userLang);
}
userLang = isSupportedLanguage(userLang) ? userLang : DEFAULT_LANGUAGE;
console.debug('Setting language to:', userLang);
this.translate.use(userLang).toPromise().then(() => {
console.debug('AppComponent language set to:', this.translate.currentLang);
});
localStorage.setItem(LANGUAGE_STORAGE_KEY, userLang);
}
} I have tried commented it out entirely and uncommented it out section by section. The part that is throwing the error is the following one: this.translate.use(userLang).toPromise().then(() => {
console.debug('AppComponent language set to:', this.translate.currentLang);
}); The resto of the code works perfectly. Thanks! |
Hi,
Please find attached code samples and test error report. ng-translate version:4.0.1 Can you please provide some documentation and code samples on how to run unit tests with ng2-translate? |
@agustincassanioe sorry for not answering to this message earlier. useFactory: (http: Http) => new TranslateStaticLoader(http, 'assets/i18n', '.json'), to this
@vikram-kerkar your problem is probably that you don't have to provide the translate service, try to remove this line: |
Closing this for now, let me know if it's not working, I'll reopen. |
I removed the translate service dependency from AppComponent and injected it into the App Module instead. This has fixed the issue. For components that use the translate pipe, I followed your setup from translate.pipe.spec.ts and everything is working fine now. |
hi @ocombe , I followed your guide, my testing setup looks like
but I still got error
I'm on ng2-translate@5.0.0 |
Hi @qiluo |
Hi there,
I have the following unit test body:
When I run unit test with "npm test" I am getting the following error stack:
I am working with ng2-translate version 2.4.3 and Angular 2 RC5.
Thanks in advance for your help!
The text was updated successfully, but these errors were encountered: