angular - 使用幻灯片进行离子单元测试会引发 TypeError
问题描述
我有一个引用我的组件,ion-slides
它在组件初始化期间引发错误:
<ion-content class="blue-purple-gradient" [fullscreen]="true">
<ion-slides pager="true" #slides>
...
</ion-slide>
</ion-slides>
export class ReEvaluationComponent implements OnInit, OnDestroy, AfterViewInit {
@ViewChild(IonSlides, {static: false}) slides: IonSlides;
...
ngAfterViewInit() {
if (this.slides) {
this.slides.lockSwipeToNext(true)
}
}
}
这在运行测试时会引发以下错误:
TypeError: Cannot read properties of undefined (reading 'apply')
这是测试:
describe('ReEvaluationComponent', () => {
beforeEach(async(() => {
NavParamsMock.setParams({
client: {dob: "1991-01-22"}
})
TestBed.configureTestingModule({
declarations: [ ReEvaluationComponent ],
imports: [
IonicModule.forRoot(),
HttpClientTestingModule,
RouterTestingModule,
FormsModule,
SharedModule,
ReactiveFormsModule
],
providers: [
{provide: Storage, useValue: storageMock},
{provide: NavParams, useValue: navParams},
{provide: WeighInsProvider, useValue: weighInsServiceMock},
{provide: GlobalsService, useValue: mockGlobalsService},
{provide: TransactionsProvider, useValue: transactionsServiceMock},
]
}).compileComponents();
}));
beforeEach(() => {
injector = getTestBed()
checkInsService = injector.get(CheckInsProvider)
weighInsService = injector.get(WeighInsProvider)
transactionsService = injector.get(TransactionsProvider)
})
beforeEach(() => {
fixture = TestBed.createComponent(ReEvaluationComponent);
component = fixture.componentInstance;
fixture.detectChanges();
})
it('should create', () => {
expect(component).toBeTruthy();
});
})
解决方案
Ion-slides 将在下一个 ionic 版本中被弃用,所以与其尝试像你正在做的那样实现它,我建议迁移到将成为我们下一个 'ion-slide' 的 Swiper 并且不会给你带来那么多麻烦。
如果您想了解更多信息:
https://ionicframework.com/docs/api/slides#migration
您可以查看此视频解释使用:
https://www.youtube.com/watch?v=XcvieKvmI5A
或者按照这个教程:
推荐阅读
- python - 为什么我会收到此消息?= FileNotFoundError: [WinError 3] 系统找不到指定的路径:XXXXX
- c++ - Cmake - include_directory ...但排除一些?
- javascript - 引导 5 模态。在提交时打开一个错误模式,而不是提供返回上一个模式或前进到下一个模式
- python - 从两个不同的表中找到最小值最大值
- r - 分母 自由度 Anova 混合设计 Type III
- kml - 更新 Google KMZ 弹出内容并删除 FME 中的“方向”
- windows - 如何将 Jenkins 管道变量用于 powershell 或 cmd 代码?
- apache-spark - 在连接列的*子集*上分区时,无需随机播放的 Spark 连接
- javascript - 如何过滤掉 MarkLogic 中的非 json 文档?
- html - 为什么文本显示为单行而不是 4 行?