我正在使用angular 9,并在深入研究后替换了angular的默认单元测试给旁观者和jest,它很有趣,我有几个测试运行良好,但突然我有这个问题,当我试图测试一些父组件,产生丑陋的错误(见问题标题),我不知道如何解决,将很高兴得到一些帮助,代码如下
其他类似的测试也在运行,但这个测试在标题上得到了不友好的消息,即使我删除了模板的所有标记,只编写了愚蠢的div,并取消了测试上的模仿,但我仍然得到了这个令人沮丧的……已尝试在没有帮助的情况下重新启动服务器。
spec.ts
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();
});
});component.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>发布于 2020-02-23 03:50:02
我不能理解你测试中的这一行
declarations: [MockComponent(SlotsListComponent)]您没有在PlannerScreenComponent中使用SlotListComponent,因此没有必要在测试中模拟此组件。我认为,您可以自由地删除这一声明行。在此之后,测试工作正常。
https://stackoverflow.com/questions/60271894
复制相似问题