简体   繁体   English

DynamicTestModule 在 angular 单元测试时抛出错误

[英]DynamicTestModule throws error on angular unit testing

I'm currently creating a unit testing with Karma and Jasmine as:我目前正在使用 Karma 和 Jasmine 创建单元测试:

describe('ProfileListComponent', () =>  {
let component: ProfileListComponent;

beforeEach(async(() => {
    TestBed.configureTestingModule({
        imports: [
            [RouterModule.forRoot([])],
            FormsModule
        ],
        declarations: [
            ProfileListComponent,
            ProfilesFiltersComponent,
            DatatableComponent,
            ProfileImagePopoverComponent,
            NgbHighlight,
            NgbRating,
            PaginationFooterComponent
        ],
        providers: []
    }).compileComponents();

    TestBed.configureTestingModule({
        declarations: [ ProfileListComponent ]
    })
    .createComponent(ProfileListComponent);
}));


it('should be 4', async(() => {
    expect(component.add(2,2)).toEqual(4);
}))

});

The profile list component has many childs, which is why I imported them on declarations .配置文件列表组件有很多子项,这就是我在declarations中导入它们的原因。 The ProfilesFiltersComponent uses angular Forms as: ProfilesFiltersComponent使用 angular Forms 作为:

HTML: HTML:

TS TS

export class ProfilesFiltersComponent implements OnInit {
 filterForm: FormGroup;
}

So, the testing asking me for the form group as:因此,测试要求我提供表单组:

Can't bind to 'formGroup' since it isn't a known property of 'form'无法绑定到“formGroup”,因为它不是“form”的已知属性

So I added it to declarations on the testing as:所以我将它添加到测试declarations中:

 declarations: [
    ...
    FormGroup
    ]

But now it is throwing a new error:但现在它抛出一个新错误:

Failed: Unexpected value 'FormGroup' declared by the module 'DynamicTestModule'.失败:模块“DynamicTestModule”声明的意外值“FormGroup”。 Please add a @Pipe/@Directive/@Component annotation.请添加 @Pipe/@Directive/@Component 注解。

That I have no idea how to solve;我不知道如何解决; what am I doing wrong?我究竟做错了什么?

Remove FormGroup from declarations .declarations中删除FormGroup

Try changing the imports to this:尝试将导入更改为此:

imports: [
            RouterTestingModule,
            // You either need FormsModule or ReactiveFormsModule
            FormsModule,
            ReactiveFormsModule
        ],

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 2单元测试 - 获取错误无法加载'ng:///DynamicTestModule/module.ngfactory.js' - Angular 2 unit testing - getting error Failed to load 'ng:///DynamicTestModule/module.ngfactory.js' 角单元测试错误:模块“ DynamicTestModule”导入了意外值“ ObservableMedia” - Angular unit test error: Unexpected value 'ObservableMedia' imported by the module 'DynamicTestModule' 单元测试角度指令抛出访问 XMLHttpRequest ...错误 - unit testing angular directive throws Access to XMLHttpRequest …error Angular 6单元测试detectChange使用@input引发组件错误 - Angular 6 unit testing detectChange throws error for component with @input 使用 Spectator 进行 Angular 单元测试 - 模块“DynamicTestModule”导入的意外值“FormBuilder”。 请添加@NgModule 注释 - Angular Unit Testing with Spectator - Unexpected value 'FormBuilder' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation Angular 单元测试:失败:模块“DynamicTestModule”导入的意外值“DxTemplateHost”:添加@NgModule 注释 - Angular Unit testing : Failed: Unexpected value 'DxTemplateHost' imported by the module 'DynamicTestModule': Add a @NgModule annotation NullInjectorError: StaticInjectorError(DynamicTestModule) 在 Angular 2 中测试时 - NullInjectorError: StaticInjectorError(DynamicTestModule) When Testing in Angular 2 Angular 2.0.0 - 测试“由模块导入'DynamicTestModule'” - Angular 2.0.0 - Testing “ imported by the module 'DynamicTestModule' ” 单元测试错误 - 失败:Angular 中模块“DynamicTestModule”声明的意外值“[object Object]” - Unit test error - Failed: Unexpected value '[object Object]' declared by the module 'DynamicTestModule' In Angular Angular 4 单元测试错误 - 无法在“XMLHttpRequest”上执行“发送”:无法加载“ng:///DynamicTestModule/LoginComponent.ngfactory.js” - Angular 4 Unit test error - Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'ng:///DynamicTestModule/LoginComponent.ngfactory.js'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM