使用NgbDatepicker的角度单元测试失败

red*_*r13 3 unit-testing datepicker ng-bootstrap angular

这看起来很简单,我确信我不会在某处导入某些东西,但我已经尝试了所有模块,包括测试,但没有运气.测试失败,出现此错误:

"exportAs"设置为"ngbDatepicker"没有指令(占位符="一年前"[(ngModel)] ="fromDate"ngbDatepicker [错误 - >] #fromDateToggle ="ngbDatepicker"title ="选择日期">

我有一个组件模板,ng-bootstrap在弹出窗口中使用datepicker(根据他们的文档)

我的模板代码如下:

<div class="input-group">
  <input id="to-date" name="dp" class="form-control" placeholder="yyyy-mm-dd" name="dp" [(ngModel)]="toDate"
    ngbDatepicker #toDateToggle="ngbDatepicker" title="Select a date">
  <button class="input-group-addon" (click)="toDateToggle.toggle()" type="button">
    <span class="fa fa-calendar"></span>
  </button>
</div>
Run Code Online (Sandbox Code Playgroud)

我已经NgbModule.forRoot()在我的spec文件中app.module.ts导入NgbModule了,这似乎是指南所需的全部内容.

我的spec文件:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { DebugElement, NO_ERRORS_SCHEMA } from '@angular/core';
import { By } from '@angular/platform-browser';
import { NgbModule, NgbActiveModal, NgbDateStruct } from '@ng-bootstrap/ng-bootstrap';
import { ExtractModalComponent } from './extract-modal.component';

fdescribe('ExtractModalComponent', () => {
  let component: ExtractModalComponent;
  let fixture: ComponentFixture<ExtractModalComponent>;
  let debugEl: DebugElement;

  beforeEach(
    async(() => {
      TestBed.configureTestingModule({
        declarations: [ExtractModalComponent],
        providers: [NgbActiveModal],
        schemas: [NO_ERRORS_SCHEMA]
      }).compileComponents();
    })
  );

  beforeEach(() => {
    fixture = TestBed.createComponent(ExtractModalComponent);
    component = fixture.componentInstance;
    debugEl = fixture.debugElement.query(By.css('.modal-body'));
    fixture.detectChanges();
  });

  fit('should create component', () => {
    expect(component).toBeTruthy();
  });
});
Run Code Online (Sandbox Code Playgroud)

72G*_*2GM 7

"我在app.module.ts中有NgbModule.forRoot()"

....这与你的测试无关.您需要在spec文件中执行此操作

例如 imports:[NgbModule.forRoot()]

值得注意的是,您不必导入完整的NgbModuleie:您可以导入NgbDatepickerModule...但我会先修复它然后再修改它