经过深入研究,我正在使用angular 9,并将angular的默认单元测试替换为旁观者和小丑,这很有趣,我几乎没有什么测试可以正常工作,但突然我在尝试测试某些父组件时遇到了这个问题,导致了我不知道如何解决的丑陋错误(见问题标题(,我很高兴得到一些帮助,代码在下面
其他类似的测试也在运行,但这一次在标题上得到了不友好的消息,即使我删除了模板的所有标记,只写了愚蠢的div,并取消了测试中的嘲笑,我仍然会得到这个糟糕的消息。。。尝试在没有任何帮助的情况下重新启动服务器。
规范
import { Spectator, createComponentFactory } from '@ngneat/spectator/jest';
import { PlannerScreenComponent } from './planner-screen.component';
import { SlotsListComponent } from '../planner/components/slots-list/slots-list.component';
import { MockComponent } from 'ng-mocks';
describe('PlannerScreenComponent suit', () => {
let spectator: Spectator<PlannerScreenComponent>;
const createComponent: () => Spectator<PlannerScreenComponent> = createComponentFactory({
component: PlannerScreenComponent,
declarations: [MockComponent(SlotsListComponent)]
});
beforeEach(() => (spectator = createComponent()));
it('should create plannerScreenComponent ', () => {
const plannerScreenComponent = spectator.component;
expect(plannerScreenComponent).toBeTruthy();
});
});
组件.ts
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'planner-screen',
templateUrl: './planner-screen.component.html',
styleUrls: ['./planner-screen.component.scss']
})
export class PlannerScreenComponent implements OnInit {
@Input() isSplittedScreen = false;
constructor() {}
ngOnInit() {}
}
component.html
section [class.splitted-screen]="isSplittedScreen" class="planner-screen-wrapper">
<aside class="left-ruller-container"></aside>
<div class="top-middle-title">
<div>title</div>
</div>
<div class="main-panel-container">
<slots-list></slots-list>
</div>
<aside class="right-ruller-container"></aside>
</section>
我无法理解您的测试中的这一行
declarations: [MockComponent(SlotsListComponent)]
您没有在PlannerScreenComponent中使用SlotListComponent,因此没有必要在测试中模拟该组件。我认为,你可以自由地删除这条声明行。之后,测试工作正常。