我正在开发一台github repository(Angular 7和Angular -CLI),我有一些测试,卡玛和茉莉在主分支工作.

现在我试图添加延迟加载特性,问题是,以前通过的测试现在没有.这很有趣,因为只有来自延迟加载模块的测试失败了...

以下是代码和错误:

import {async, TestBed} from '@angular/core/testing';
import {APP_BASE_HREF} from '@angular/common';
import {AppModule} from '../../app.module';
import {HeroDetailComponent} from './hero-detail.component';

describe('HeroDetailComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [AppModule
      ],
      providers: [
        {provide: APP_BASE_HREF, useValue: '/'}
      ],
    }).compileComponents();
  }));

  it('should create hero detail component', (() => {
    const fixture = TestBed.createComponent(HeroDetailComponent);
    const component = fixture.debugElement.componentInstance;
    expect(component).toBeTruthy();
  }));
});

错误在于:

Chrome 58.0.3029 (Mac OS X 10.12.6) HeroDetailComponent should create hero detail component FAILED
    Error: Illegal state: Could not load the summary for directive HeroDetailComponent.
        at syntaxError Users/ismael.ramos.silvan/WebstormProjects/angular4-example-app/~/@angular/compiler/@angular/compiler.es5.js:1690:22)
        at CompileMetadataResolver.getDirectiveSummary Users/ismael.ramos.silvan/WebstormProjects/angular4-example-app/~/@angular/compiler/@angular/compiler.es5.js:15272:1)
        at JitCompiler.getComponentFactory Users/ismael.ramos.silvan/WebstormProjects/angular4-example-app/~/@angular/compiler/@angular/compiler.es5.js:26733:26)
        at TestingCompilerImpl.getComponentFactory Users/ismael.ramos.silvan/WebstormProjects/angular4-example-app/~/@angular/compiler/@angular/compiler/testing.es5.js:484:1)
        at TestBed.createComponent Users/ismael.ramos.silvan/WebstormProjects/angular4-example-app/~/@angular/core/@angular/core/testing.es5.js:874:1)
        at Function.TestBed.createComponent Users/ismael.ramos.silvan/WebstormProjects/angular4-example-app/~/@angular/core/@angular/core/testing.es5.js:652:1)
        at UserContext.it Users/ismael.ramos.silvan/WebstormProjects/angular4-example-app/src/app/heroes/hero-detail/hero-detail.component.spec.ts:18:29)
        at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke Users/ismael.ramos.silvan/WebstormProjects/angular4-example-app/~/zone.js/dist/zone.js:391:1)
        at ProxyZoneSpec.onInvoke Users/ismael.ramos.silvan/WebstormProjects/angular4-example-app/~/zone.js/dist/proxy.js:79:1)
        at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke Users/ismael.ramos.silvan/WebstormProjects/angular4-example-app/~/zone.js/dist/zone.js:390:1)

您可以查看整个项目,如果需要,可以查看更多详细信息.

UPDATE: added declaration like this:

beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        AppModule
      ],
      declarations: [HeroDetailComponent],
      providers: [
        {provide: APP_BASE_HREF, useValue: '/'}
      ],
    }).compileComponents();
  }));

现在,出现了新的错误:

The pipe 'translate' could not be found ("<h1 class="section-title">{{[ERROR ->]'heroDetail' | translate}}</h1>
    <md-progress-spinner *ngIf="!hero"
                         class="progre"): ng:///DynamicTestModule/HeroDetailComponent.html@0:28
    Can't bind to 'color' since it isn't a known property of 'md-progress-spinner'.

还有更多...就像Angle material中的所有指令和组件一样,ngx translate/core中的管道平移似乎不包括在内...

推荐答案

您在没有首先声明组件的情况下通过了HeroDetailComponentTestBed.createComponent():

TestBed.configureTestingModule({
  imports: [AppModule,
     CommonModule,
     FormsModule,
     SharedModule,
     HeroRoutingModule,
     ReactiveFormsModule
  ],
  providers: [
    {provide: APP_BASE_HREF, useValue: '/'}
  ],
  declarations: [HeroDetailComponent]
}).compileComponents();

希望有帮助.


更新测试中的以下错误:添加了更多导入(只需将Hero模块作为蓝图,因为这基本上就是您想要导入和提供的).

Angular相关问答推荐

如何动态呈现组件并以Angular 访问它们的方法?

我们应该在Angular 从14升级到15的过程中也升级茉莉花和业力相关的依赖吗?

有Angular react 形式的输入用货币管

无法匹配任何路由.URL段:';英雄';

如何在Primeng中实现自定义排序

ngx-http-client 在 Angular 16 中已弃用

从 Angular 应用程序中删除散列标签后无法从 Apache 访问 API

Angular 升级后 RXJS SwitchMap 无法与解析器一起正常工作

http 调用的 RxJs 数组

PrimeNG:如何以编程方式更改分页器中的选定页面?

如何重置表单中的特定字段?

在 angular2 中,如何获取在为 @Input 发送的对象上更改的属性的 onChanges

angular material日期 Select 器中的日期不正确

如何在 Angular 2 中使用 protractor ?

缺少 .angular-cli.json 文件:Angular

ngx-bootstrap modal:如何从模态中获取返回值?

angular 2模板使用console.log

NgrxStore 和 Angular - 大量使用异步管道或在构造函数中只订阅一次

Angular2 Base64 清理不安全的 URL 值

如何将 @Input 与使用 ComponentFactoryResolver 创建的组件一起使用?