angular - 使用 Jasmine-Karma 在 Ionic 3 中进行组件单元测试
问题描述
我正在尝试在 Ionic 3 中测试一个页面。因此,我在 src/pages/page 文件夹中手动创建了该页面的 .spec.ts 。但是当我使用 TestBed.createComponent 创建组件时,它会显示以下错误:
about.spec.ts 文件
import { CUSTOM_ELEMENTS_SCHEMA } from "@angular/core";
import { TestBed, ComponentFixture, async } from "@angular/core/testing";
import { AboutPage } from './about';
import { IonicPageModule, NavController, NavParams, Platform } from 'ionic-angular';
import { BrowserDynamicTestingModule,platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
describe('AboutPage', () => {
let fixture;
let component;
beforeEach(async(() => {
TestBed.resetTestEnvironment();
TestBed.initTestEnvironment(BrowserDynamicTestingModule,platformBrowserDynamicTesting());
TestBed.configureTestingModule({
declarations: [ AboutPage ],
imports: [IonicPageModule.forChild(AboutPage)],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
providers: [NavController, NavParams, Platform]
}).compileComponents().then((response) => {
fixture = TestBed.createComponent(AboutPage);
component = fixture.componentInstance;
fixture.detectChanges();
});
}));
afterEach(() => {
fixture.destroy();
component = null;
});
describe('First test',() => {
it('should print',() => {
let a = true;
expect(a).toBeTruthy();
})
})
});
我无法找出我的代码缺少的地方,因为这个错误即将到来。我需要在我的规范文件中获取组件,以便对其功能及其部分进行测试。请提出我可能做错的地方。
建议(第 1 版):
我通过删除 afterEach 并将 beforeEach 代码重构为两部分来更改了 about.spec.ts:
import { CUSTOM_ELEMENTS_SCHEMA ,NO_ERRORS_SCHEMA} from "@angular/core";
import { TestBed, ComponentFixture, async } from "@angular/core/testing";
import { AboutPage } from './about';
import { IonicPageModule, NavController, NavParams, Platform } from 'ionic-angular';
import { BrowserDynamicTestingModule,platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
describe('AboutPage', () => {
let fixture;
let component;
beforeEach(async(() => {
TestBed.initTestEnvironment(BrowserDynamicTestingModule,platformBrowserDynamicTesting());
TestBed.configureTestingModule({
declarations: [ AboutPage ],
imports: [IonicPageModule.forChild(AboutPage)],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
providers: [NavController, NavParams, Platform]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(AboutPage);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should print',() => {
let a = true;
expect(a).toBeTruthy();
});
});
但是我再次遇到相同类型的错误,而不是破坏错误。
请建议。
解决方案
错误来自fixture.destroy()
内部afterEach
函数,因为fixture
is undefined
。
你可以摆脱这个afterEach
函数,因为fixture
并且component
应该在beforeEach
函数内部再次为每个测试创建。要真正实现这一点,还请尝试将其拆分为与CLI 生成的测试beforeEach
中相同的两个函数。
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ AboutPage ],
imports: [IonicPageModule.forChild(AboutPage)],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
providers: [NavController, NavParams, Platform]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(AboutPage);
component = fixture.componentInstance;
fixture.detectChanges();
});
推荐阅读
- certificate - CA 证书可以导入受信任的人存储吗?
- git - openshift 中的 git clone 失败“不存在具有 uid 的用户”
- c# - 检查智能卡读卡器是否打开
- firebase - 颤振与 Firebase 错误-signInWithEmailAndPassword 不起作用
- javascript - nunjucks:错误:找不到模板,目录问题
- android - 无需在真实设备上运行测试的 UI 测试
- angular - 如何初始化 Angular 基于角色的安全性?
- java - 无法播放路径中的视频 (/storage/emulated/0/Android/data/com...)
- python - 如何在scrapy中添加一个描述值来自哪个链接的值
- node.js - Angular Node.js CPanel 部署