Ste*_*ane 6 typescript angular angular9
我正在使用该ng test命令运行一些测试,并收到以下错误消息:
NullInjectorError: R3InjectorError(DynamicTestModule)[TranslateService -> TranslateService]:
NullInjectorError: No provider for TranslateService!
Run Code Online (Sandbox Code Playgroud)
测试是:
import { TestBed, async, ComponentFixture } from '@angular/core/testing';
import { AppComponent } from './app.component';
import { TranslateTestingModule } from 'ngx-translate-testing';
import { CoreModule } from './core.module';
const ENGLISH_LANGUAGE = 'en';
describe('AppComponent', () => {
let fixture: ComponentFixture<AppComponent>;
let appComponent: AppComponent;
beforeEach(() => {
fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
appComponent = fixture.componentInstance;
});
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent
],
imports: [
CoreModule,
TranslateTestingModule
.withTranslations(ENGLISH_LANGUAGE, require('assets/i18n/en.json'))
.withDefaultLanguage(ENGLISH_LANGUAGE)
],
}).compileComponents();
});
it('should create the app', async(() => {
expect(appComponent).toBeTruthy();
}));
it('should have as title', async(() => {
appComponent.ngOnInit();
fixture.whenStable().then(() => {
expect(appComponent.testTitle).toEqual('app');
})
}));
});
Run Code Online (Sandbox Code Playgroud)
组件是:
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
testTitle?: string;
constructor(
private translateService: TranslateService,
private screenDeviceService: ScreenDeviceService
) {}
public ngOnInit() {
const subscription: Subscription = this.translateService.get('app.title').subscribe((text: string) => {
this.afterLanguageResourcesLoaded();
subscription.unsubscribe();
});
}
private afterLanguageResourcesLoaded(): void {
this.setAppMetaData();
}
private setAppMetaData(): void {
this.screenDeviceService.setMetaData({
title: this.translateService.instant('app.title'),
description: this.translateService.instant('app.description')
});
this.testTitle = this.translateService.instant('app.title');
}
}
Run Code Online (Sandbox Code Playgroud)
我也尝试导入BrowserModule模块:
imports: [
BrowserAnimationsModule,
Run Code Online (Sandbox Code Playgroud)
我尝试添加一个TranslateService提供者:
providers: [
TranslateService
]
Run Code Online (Sandbox Code Playgroud)
我终于尝试将两个服务的两个成员变量注入到组件构造函数中:
let translateService: TranslateService;
let screenDeviceService: ScreenDeviceService;
Run Code Online (Sandbox Code Playgroud)
并为他们提供:
providers: [
TranslateService,
ScreenDeviceService
]
Run Code Online (Sandbox Code Playgroud)
并获得它们:
translateService = TestBed.get(TranslateService);
screenDeviceService = TestBed.get(ScreenDeviceService);
Run Code Online (Sandbox Code Playgroud)
但没有任何帮助。
我在 Angular 下 9.0.4
更新:我最终使用了真正的翻译服务而不是模拟的翻译服务,并且不得不杀死测试运行器并再次输入ng test要考虑我的源代码的命令。
San*_*man 10
在您的测试 spec.ts 文件中,您可以添加这种类型的导入,使用forRoot():
TranslateModule.forRoot()
Run Code Online (Sandbox Code Playgroud)
一个例子:
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule,
TranslateModule.forRoot(),
HttpClientTestingModule
],
declarations: [ HomeComponent ],
schemas: [ NO_ERRORS_SCHEMA ]
})
.compileComponents();
}));
Run Code Online (Sandbox Code Playgroud)
这是因为您还没有测试 TranslateModule。这个解决方案对我有用。
| 归档时间: |
|
| 查看次数: |
3421 次 |
| 最近记录: |