我在使用Jasmine测试我的angular 6组件时出现错误:
找不到管道'paginate‘(“] activityLogDisplay.data | paginate: config;let index = index”(单击)=“”):ng:///DynamicTestModule/ActivitylogComponent.html@59:34 'pagination-controls’不是已知元素:
我使用的是通过以下命令安装的NgxPaginationModule:
npm install ngx-pagination --save请注意,我使用的不是我自己创建的自定义管道,我使用的是属于我下载的包的管道。我的组件测试在使用管道时失败,而不是在测试管道本身时失败。下面是我在html模板中对分页管道的使用:
<li class="list-group-item list-group-item-action"
*ngFor="let alItem of activityLogDisplay.data | paginate: config; let index = index"
(click)="toggleALView(alItem.index)">下面是我的spec.ts文件的样子(几乎开箱即用):
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ActivitylogComponent } from './activitylog.component';
describe('ActivitylogComponent', () => {
let component: ActivitylogComponent;
let fixture: ComponentFixture<ActivitylogComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ ActivitylogComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ActivitylogComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});我已经尝试在spec.ts文件中添加NgxPaginationModule导入:
import { NgxPaginationModule } from 'ngx-pagination';我已经将它包含在TestBed配置的声明区域中:
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ ActivitylogComponent, NgxPaginationModule ]
})
.compileComponents();
}));错误消息更改为:
Failed: Unexpected module 'NgxPaginationModule' declared by the module
'DynamicTestModule'. Please add a @Pipe/@Directive/@Component annotation.在我的app.module.ts文件中,我在imports数组中列出了NgxPaginationModule,而不是在声明数组中:
import { NgxPaginationModule } from 'ngx-pagination';
@NgModule({
declarations: [
AppComponent,
ActivitylogComponent
],
imports: [
BrowserModule,
FormsModule,
HttpClientModule,
NgxPaginationModule,
RouterModule.forRoot(appRoutes,{useHash:true})
],
providers: [IndexService, AdminIndexService],
bootstrap: [LandingpageComponent]
})你知道我需要做什么才能让这个测试工作吗?
发布于 2019-06-21 23:21:11
解决了我的问题。我需要将NgxPaginationModule作为导入添加到我的规范文件中的测试床configureTestingModule区域:
beforeEach(async(() => {
TestBed.configureTestingModule({
imports : [
NgxPaginationModule
],
declarations: [
ActivitylogComponent
]
})
.compileComponents();
}));https://stackoverflow.com/questions/56686358
复制相似问题