【问题标题】:Angular Unit Test using NgbDatepicker is failing使用 NgbDatepicker 的角度单元测试失败
【发布时间】:2018-01-26 21:02:01
【问题描述】:

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

没有将“exportAs”设置为“ngbDatepicker”的指令 (placeholder="一年前" [(ngModel)]="fromDate" ngbDatepicker [ERROR->]#fromDateToggle="ngbDatepicker" title="选择日期">

我有一个在弹出窗口中使用ng-bootstrap datepicker 的组件模板(根据他们的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();
  });
});

【问题讨论】:

    标签: angular unit-testing datepicker ng-bootstrap


    【解决方案1】:

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

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

    例如imports:[NgbModule.forRoot()]

    值得注意的是,您不必导入完整的 NgbModule,即:您可以只导入 NgbDatepickerModule ...但我会先让它工作,然后再修改它

    【讨论】:

    • 谢谢。我仍然没有很好地处理所有需要放入规范文件本身的内容,以及从任何其他模块中获取的内容(如果有的话)。
    • 非常感谢! .spec.ts 文件中的imports:[NgbModule.forRoot()] 修复了它。虽然我尝试了import { NgbDatepickerModule} from '@ng-bootstrap/ng-bootstrap/datepicker/datepicker.module';,但错误又回来了。
    • 要导入单个项目,请执行以下操作L import { NgbDatepickerModule } from '@ng-bootstrap/ng-bootstrap';然后导入: [ NgbDatepickerModule .forRoot() ]
    • 我在类型 'typeof NgbModule' 上出现'Property 'forRoot' 错误
    • 因为他们在 2019 年删除了它
    猜你喜欢
    • 2018-11-18
    • 1970-01-01
    • 2023-01-22
    • 2018-05-03
    • 2021-05-25
    • 2021-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多