Angular 使用 NgbDatepicker 的单元测试失败
Angular Unit Test using NgbDatepicker is failing
这看起来很简单,我确定我没有在某处导入某些东西,但我已经尝试了所有模块,包括测试,但没有成功。测试失败并出现此错误:
There is no directive with "exportAs" set to "ngbDatepicker"
(placeholder="a year ago" [(ngModel)]="fromDate" ngbDatepicker [ERROR->]#fromDateToggle="ngbDatepicker" title="Select a date">
我有一个组件模板,它在弹出窗口中使用 ng-bootstrap
日期选择器(根据他们的 docs)
我的模板代码如下:
<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>
我在 app.module.ts
中有 NgbModule.forRoot()
并且在我的规范文件中导入了 NgbModule
,这似乎是 guide 所需要的。
我的规范文件:
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();
});
});
"I have NgbModule.forRoot() in app.module.ts"
...这与您的测试无关。您需要在规范文件中执行此操作
例如imports:[NgbModule.forRoot()]
值得注意的是,您不必导入完整的 NgbModule
即:您可以只导入 NgbDatepickerModule
...但我会先让它工作,然后再修改它
这看起来很简单,我确定我没有在某处导入某些东西,但我已经尝试了所有模块,包括测试,但没有成功。测试失败并出现此错误:
There is no directive with "exportAs" set to "ngbDatepicker" (placeholder="a year ago" [(ngModel)]="fromDate" ngbDatepicker [ERROR->]#fromDateToggle="ngbDatepicker" title="Select a date">
我有一个组件模板,它在弹出窗口中使用 ng-bootstrap
日期选择器(根据他们的 docs)
我的模板代码如下:
<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>
我在 app.module.ts
中有 NgbModule.forRoot()
并且在我的规范文件中导入了 NgbModule
,这似乎是 guide 所需要的。
我的规范文件:
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();
});
});
"I have NgbModule.forRoot() in app.module.ts"
...这与您的测试无关。您需要在规范文件中执行此操作
例如imports:[NgbModule.forRoot()]
值得注意的是,您不必导入完整的 NgbModule
即:您可以只导入 NgbDatepickerModule
...但我会先让它工作,然后再修改它