Ionic3 单元测试Example(给Page增加单元测试等)

一. Angular单元测试

https://angular.io/guide/testing

进行Ionic单元测试之前,可以大概了解下Angular单元测试。这样可以帮助我们更好的进行Ionic单元测试。

二. Ionic test example

  1. 下载地址
https://github.com/ionic-team/ionic-unit-testing-example

2.测试的名字和位置


image.png

如上图: 文件的名字约定为 *.spec.ts, 文件的位置放在 page1.ts相同的路径

3.app.component.ts文件

import { async, TestBed } from '@angular/core/testing';
import { IonicModule, Platform } from 'ionic-angular';

import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';

import { MyApp } from './app.component';
import {
  PlatformMock,
  StatusBarMock,
  SplashScreenMock
} from '../../test-config/mocks-ionic';

describe('MyApp Component', () => {
  let fixture;
  let component;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [MyApp],
      imports: [
        IonicModule.forRoot(MyApp)
      ],
      providers: [
        { provide: StatusBar, useClass: StatusBarMock },
        { provide: SplashScreen, useClass: SplashScreenMock },
        { provide: Platform, useClass: PlatformMock }
      ]
    })
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(MyApp);
    component = fixture.componentInstance;
  });

  it('should be created', () => {
    expect(component instanceof MyApp).toBe(true);
  });

  it('should have two pages', () => {
    expect(component.pages.length).toBe(2);
  });

});

expect(component.pages.length).toBe(2);
该测试用例代表pages个数为2时,测试pass

  1. page1.spec.ts文件
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { By }           from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import { Page1 } from './page1';
import { IonicModule, Platform, NavController} from 'ionic-angular/index';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { PlatformMock, StatusBarMock, SplashScreenMock } from '../../../test-config/mocks-ionic';

describe('Page1', () => {
  let de: DebugElement;
  let comp: Page1;
  let fixture: ComponentFixture<Page1>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [Page1],
      imports: [
        IonicModule.forRoot(Page1)
      ],
      providers: [
        NavController,
        { provide: Platform, useClass: PlatformMock},
        { provide: StatusBar, useClass: StatusBarMock },
        { provide: SplashScreen, useClass: SplashScreenMock },
      ]
    });
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(Page1);
    comp = fixture.componentInstance;
    de = fixture.debugElement.query(By.css('h3'));
  });

  it('should create component', () => expect(comp).toBeDefined());

  it('should have expected <h3> text', () => {
    fixture.detectChanges();
    const h3 = de.nativeElement;
    expect(h3.innerText).toMatch(/ionic/i,
      '<h3> should say something about "Ionic"');
  });
});
  1. 运行单元测试
    下载Sample后,运行
npm install

安装。
然后使用下面命令,进行单元测试

npm run test
  1. 计算单元测试覆盖率
npm run test-coverage

运行后,将在工程根路径下创建coverage文件,在coverage里,打开index.html,就能够查看覆盖率


image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,269评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,638评论 25 709
  • 手机通讯里自己的电话号码叫做“建建的宝贝”,每天下午5点21分,飞信天气预报都准时的发过来“建建的宝贝”的天气预报...
    独孤思齐阅读 260评论 0 1
  • 新环境坚持的第二天,又是一个十二点到家。 当十一点走到地铁站的时候,感觉很疲惫,但地铁还是很多的人,突然跟同事感慨...
    Shona_k阅读 393评论 0 0
  • 讲两个故事,故事的当事人是身边的朋友,以下是他们的自述。 01 小A上周末酒后哭着说,累积在心底的委屈终于爆发出来...
    孤独的人晚回家阅读 582评论 0 5