首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Jasmine测试中找不到角度管道

在Jasmine测试中找不到角度管道
EN

Stack Overflow用户
提问于 2019-06-20 20:48:35
回答 1查看 3.3K关注 0票数 1

我在使用Jasmine测试我的angular 6组件时出现错误:

找不到管道'paginate‘(“] activityLogDisplay.data | paginate: config;let index = index”(单击)=“”):ng:///DynamicTestModule/ActivitylogComponent.html@59:34 'pagination-controls’不是已知元素:

我使用的是通过以下命令安装的NgxPaginationModule:

代码语言:javascript
复制
npm install ngx-pagination --save

请注意,我使用的不是我自己创建的自定义管道,我使用的是属于我下载的包的管道。我的组件测试在使用管道时失败,而不是在测试管道本身时失败。下面是我在html模板中对分页管道的使用:

代码语言:javascript
复制
 <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文件的样子(几乎开箱即用):

代码语言:javascript
复制
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导入:

代码语言:javascript
复制
import { NgxPaginationModule } from 'ngx-pagination';

我已经将它包含在TestBed配置的声明区域中:

代码语言:javascript
复制
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ ActivitylogComponent, NgxPaginationModule ]
    })
    .compileComponents();
  }));

错误消息更改为:

代码语言:javascript
复制
Failed: Unexpected module 'NgxPaginationModule' declared by the module 
'DynamicTestModule'. Please add a @Pipe/@Directive/@Component annotation.

在我的app.module.ts文件中,我在imports数组中列出了NgxPaginationModule,而不是在声明数组中:

代码语言:javascript
复制
import { NgxPaginationModule } from 'ngx-pagination';

@NgModule({
  declarations: [
    AppComponent,
    ActivitylogComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpClientModule,
    NgxPaginationModule,
    RouterModule.forRoot(appRoutes,{useHash:true})
  ],
  providers: [IndexService, AdminIndexService],
  bootstrap: [LandingpageComponent]
})

你知道我需要做什么才能让这个测试工作吗?

EN

回答 1

Stack Overflow用户

发布于 2019-06-21 23:21:11

解决了我的问题。我需要将NgxPaginationModule作为导入添加到我的规范文件中的测试床configureTestingModule区域:

代码语言:javascript
复制
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports : [
        NgxPaginationModule
      ],
      declarations: [ 
        ActivitylogComponent
       ]
    })
    .compileComponents();
  }));
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56686358

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档