angular - 模块 'DecoratorFactory' 导入的意外值 'DynamicTestModule' - karma-jasmine

标签 angular karma-jasmine angular2-testing

我在创建测试组件实例时遇到错误。

let comp: TaskviewComponent;
let fixture: ComponentFixture;
let deTaskTitle: DebugElement;
let elSub: HTMLElement;

describe('TaskviewComponent', () => {

    beforeEach( () => {
        TestBed.configureTestingModule({
            declarations: [
                TaskviewComponent
            ],
            imports: [
                NgModule,
                RouterTestingModule,
                TranslateModule.forRoot(),
            ],
            providers: [
                RestDataService,
                Restangular,
            {provide: OAuthService, useClass: OAuthServicMock},
            {provide: ComponentFixtureAutoDetect, useValue: true},
            {provide: UserInfoService, useClass: UserInfoServiceMock},
            {
                provide: LocalStorageService, //provide: LOCAL_STORAGE_SERVICE_CONFIG,
                useValue: {
                    prefix: ApplicationConstants.ANGULAR2_LOCAL_STORAGE_ID,
                    storageType: 'sessionStorage'
                }
            }],,

    })
            fixture = TestBed.createComponent(TaskviewComponent);
            comp = fixture.componentInstance;
            deTaskTitle = fixture.debugElement.query((By.css('.Subject')));
            elSub = deTaskTitle.nativeElement;

});

it('should have a subject', () => {
     expect(elSub.textContent).toContain('Client Data Maintenance2 ');
});
});

我收到错误:模块“DynamicTestModule”导入的意外值“DecoratorFactory”错误。我注意到如果我删除“fixture = TestBed.createComponent(TaskviewComponent);”错误将得到解决。但这不会创建测试组件。此外,我注意到如果我不在 imports[] 中包含 NgModule,则无法识别 Ngmodel、datepicker 等元素。

最佳答案

您不能导入“NgModule”,因为它是装饰器而不是模块。

关于angular - 模块 'DecoratorFactory' 导入的意外值 'DynamicTestModule' - karma-jasmine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41277642/

相关文章:

angular - 如何模拟 angular2 平台浏览器标题组件以进行测试

angular 5 测试组件的静态方法

javascript - 虚拟机上的 Karma 测试

Angular 7 单元测试 Observable 订阅调用

angular - 如何修复 beforeEachProviders(在 RC4 上已弃用)

angular - 使用 Angular 2 的 TestComponentBuilder 让我感到困惑

angular - ngx-translate .instant 返回键而不是值

angular - Angular 单页应用程序 (SPA) 的 Shibboleth 身份验证

angular - ion-checkbox "checked"属性不起作用

AngularJS 测试 - 测试 Promise 回调中的逻辑