简体   繁体   English

使用 Jasmine-Karma 在 Ionic 3 中进行组件单元测试

[英]Component Unit Testing in Ionic 3 using Jasmine-Karma

I am trying to test a page in Ionic 3. So, I created.spec.ts of that page manually within src/pages/page folder.我正在尝试在 Ionic 3 中测试一个页面。因此,我在 src/pages/page 文件夹中手动创建了该页面的 .spec.ts。 But when I create component using TestBed.createComponent, then it shows following error:但是当我使用 TestBed.createComponent 创建组件时,它会显示以下错误:

在此处输入图像描述

about.spec.ts file关于.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();
        })
    })
});

I am unable to find out where my code is lacking due to which this error is coming.由于出现此错误,我无法找出缺少我的代码的位置。 I need to fetch component in my spec file so as to perform testing on its functions and its part.我需要在我的规范文件中获取组件,以便对其功能及其部分进行测试。 Please suggest where I may be doing wrong.请提出我可能做错的地方。

AS SUGGESTED (Edition-1):按照建议(第 1 版):

I changed my about.spec.ts by removing afterEach and refactoring beforeEach code into 2 parts:我通过删除 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();
    });
});

But again I am getting same type of error, not that of destroy one.但是我再次遇到相同类型的错误,而不是破坏错误。

在此处输入图像描述

Please suggest.请建议。

The error is coming from fixture.destroy() inside afterEach function because fixture is undefined .错误来自 afterEach function 内afterEach fixture.destroy()因为fixtureundefined

You can get rid of this afterEach function since fixture and component are supposed to be created for each test again inside the beforeEach function. To make this really happen, also try to split the beforeEach into two functions same as found in CLI-generated tests .您可以摆脱这个afterEach function,因为fixturecomponent应该在beforeEach function 中再次为每个测试创建。要真正实现这一点,还请尝试将beforeEach拆分为两个函数,就像在CLI 生成的测试中找到的一样。

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();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM