4

I am new to Jasmine with Angular 2, I am frequently working with the TestBed object when writting a Testcase and getting the error:Please call "TestBed.compileComponents" before your test.

How do I solve this error?

@Component({
  moduleId:module.id,
    selector: 'my-app',
    templateUrl: 'app-component.html',

})
4

1 回答 1

13

请在测试前调用“TestBed.compileComponents”

使用测试组件时需要此调用templateUrl

错误:无法创建组件 AppComponent,因为它没有导入到测试模块中!

您需要TestBed在每次测试之前进行配置,添加测试所需的任何组件、模块和服务。这就像@NgModule从头开始配置常规一样,但您只需添加您需要的内容。

import { async, TestBed } from '@angular/core/testing';

beforeEach(async(() => {
  TestBed.configureTestingModule({
    declarations: [ AppComponent ],
    providers: [],
    imports: []
  })
  .compileComponents();
}));

it('...', () => {
  let fixture = TestBed.createComponent(AppComponent);
});

也可以看看

于 2016-10-06T08:32:34.713 回答