【发布时间】:2019-09-27 18:29:51
【问题描述】:
在创建新组件后,我几乎在所有测试中都遇到了这个错误:
SampleComponent > should create
Failed: Component TuzGraphComponent is not part of any NgModule or the module has not been imported into your module.
Error: Component TuzGraphComponent is not part of any NgModule or the module has not been imported into your module.
at JitCompiler._createCompiledHostTemplate (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/compiler/fesm2015/compiler.js:25493:1)
at http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/compiler/fesm2015/compiler.js:25469:1
at <Jasmine>
at http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/compiler/fesm2015/compiler.js:25466:1
at <Jasmine>
at JitCompiler._compileComponents (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/compiler/fesm2015/compiler.js:25455:1)
at http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/compiler/fesm2015/compiler.js:25393:1
at Object.then (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/compiler/fesm2015/compiler.js:2166:27)
at JitCompiler._compileModuleAndAllComponents (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/compiler/fesm2015/compiler.js:25391:1)
at JitCompiler.compileModuleAndAllComponentsAsync (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/compiler/fesm2015/compiler.js:25353:1)
Error: Component TuzGraphComponent is not part of any NgModule or the module has not been imported into your module.
Error: Component TuzGraphComponent is not part of any NgModule or the module has not been imported into your module.
at JitCompiler._createCompiledHostTemplate (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/compiler/fesm2015/compiler.js:25493:1)
at http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/compiler/fesm2015/compiler.js:25469:1
at <Jasmine>
at http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/compiler/fesm2015/compiler.js:25466:1
at <Jasmine>
at JitCompiler._compileComponents (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/compiler/fesm2015/compiler.js:25455:1)
at http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/compiler/fesm2015/compiler.js:25393:1
at Object.then (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/compiler/fesm2015/compiler.js:2166:27)
at JitCompiler._compileModuleAndAllComponents (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/compiler/fesm2015/compiler.js:25391:1)
at JitCompiler.compileModuleAndAllComponentsSync (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/compiler/fesm2015/compiler.js:25350:1)
Expected undefined to be truthy.
Error: Expected undefined to be truthy.
at <Jasmine>
at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/webpack:/src/app/sandboxviews/sample/sample.component.spec.ts:85:23)
at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-evergreen.js:359:1)
at ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:308:1)
所以我将TuzGraphComponent 添加到我所有测试的声明中。
首先,必须有比手动添加任何新组件到每个现有测试文件更简单的方法?!?!
无论如何,这修复了大部分测试。仍然有几个文件有相同的错误,但是当我打开文件并比较它们时,故障消失了。我真的不知道发生了什么变化。在其中一个文件中,我只是在 Webstorm 中点击“重新格式化文件”,这似乎可以修复它,在另一个文件中,我将位于其自身行上的 { 移回其上方的行(结果:({)和这似乎解决了它。
但是仍然有一个测试文件失败,同样的错误;这实际上是我在上面发布的错误。
我将把那个文件粘贴到这里,也许其他人能发现其中的区别?
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {AccessdeniedComponent} from '../../pages/accessdenied/accessdenied.component';
import {AppMainComponent} from '../../app.main.component';
import {AppSubMenuComponent, LeftmenuComponent} from '../../leftmenu/leftmenu.component';
import {CalibrationComponent} from '../calibration/calibration.component';
import {ControlComponent} from '../control/control.component';
import {DataviewerComponent} from '../dataviewer/dataviewer.component';
import {ExperimentComponent} from '../experiment/experiment.component';
import {FilemanagerComponent} from '../filemanager/filemanager.component';
import {FooterComponent} from '../../footer/footer.component';
import {HeaderbarComponent} from '../../headerbar/headerbar.component';
import {LoginComponent} from '../../pages/login/login.component';
import {MethodComponent} from '../method/method.component';
import {NotfoundComponent} from '../../pages/notfound/notfound.component';
import {TuneComponent} from '../tune/tune.component';
import {AccordionModule} from 'primeng/accordion';
import {AppRoutingModule} from '../../app-routing.module';
import {BlockUIModule} from 'primeng/blockui';
import {CheckboxModule} from 'primeng/checkbox';
import {DialogModule} from 'primeng/dialog';
import {DropdownModule} from 'primeng/dropdown';
import {FormsModule} from '@angular/forms';
import {InputSwitchModule} from 'primeng/inputswitch';
import {InputTextModule} from 'primeng/inputtext';
import {OverlayPanelModule} from 'primeng/overlaypanel';
import {SampleComponent} from './sample.component';
import {ScrollPanelModule, SpinnerModule} from 'primeng/primeng';
import {TableModule} from 'primeng/table';
import {TuzGraphComponent} from '../tuz-graph/tuz-graph.component';
describe('SampleComponent', () => {
let component: SampleComponent;
let fixture: ComponentFixture<SampleComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AccessdeniedComponent,
AppMainComponent,
AppSubMenuComponent,
CalibrationComponent,
ControlComponent,
DataviewerComponent,
ExperimentComponent,
FilemanagerComponent,
FooterComponent,
HeaderbarComponent,
LeftmenuComponent,
LoginComponent,
MethodComponent,
NotfoundComponent,
SampleComponent,
TuneComponent,
TuzGraphComponent
],
imports: [
AccordionModule,
AppRoutingModule,
BlockUIModule,
CheckboxModule,
DialogModule,
DropdownModule,
FormsModule,
InputSwitchModule,
InputTextModule,
OverlayPanelModule,
ScrollPanelModule,
SpinnerModule,
TableModule
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(SampleComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
【问题讨论】:
标签: angular jasmine karma-runner