Ionic2,Jasmine,Karma单元测试

时间:2017-01-12 13:34:25

标签: jasmine ionic2 karma-jasmine

我们正在为Ionic2项目建立一个单元测试框架 在运行npm测试时,我们遇到以下错误。 我在下面给出src / .ts和src / .spec.ts以及cmd窗口错误
请帮忙解决

  

Chrome 55.0.2883(Windows 8.1 0.0.0)错误:错误{rejection:'失败   加载app.html',承诺:ZoneAwarePromise {__ zone_symbol__state:0,   __zone_symbol__value:'无法加载app.html'},z one:Zone {_properties:Object {},_ parent:null,_name:'',   _zoneDelegate:ZoneDelegate {_t askCounts:...,zone:...,_ parentDelegate:...,_ forkZS:...,_ forkDlgt:...,_ interceptZS:..,_ interceptDlgt:...,_ invokeZS :. ..,_ invokeDlgt:...,   _handleErrorZS:...,_ handle_Llgt:...,_ scheduleTaskZS:...,_scheduleTaskDlgt:...,_ invokeTaskZS:...,_ invokeTaskDlgt:...,   _cancelTask​​ZS:...,_ cancelTask​​Dlgt:...,_ hasTaskZS:...,_ hasTaskDlgt:...}},task:ZoneTask {runCount:1,type:'microTask',zone:Zone {_properties:..., _parent:...,_ name:...,_ zoneDel egate:   ...},source:'Promise.then',data:undefined,scheduleFn:undefined,   cancelFn:null,ca llback:function(){...},invoke:function(){   ......}}}

     

Chrome 55.0.2883(Windows 8.1 0.0.0)DashboardService应该返回一个   非空数组FAILED TypeError:无法读取属性'assertPresent'   在resetFakeAsyncZone中未定义   (C:/用户/用户/应用程序数据/本地/温度/果报打字稿束   -66485eOt1ZMUGTWZ.js:143884:22)at Object。 (C:/用户/用户/应用程序数据/本地/温度/果报打字稿束   -66485eOt1ZMUGTWZ.js:144536:13)错误:async()测试助手需要ProxyZoneSpec但找不到。请确保这一点   您的环境包括runInTestZone中的zone.js / dist / proxy.js   (C:/用户/用户/应用程序数据/本地/温度/果报打字稿束-6648   5eOt1ZMUGTWZ.js:143659:19)at Object。   (C:/用户/用户/应用程序数据/本地/温度/果报打字稿束   -66485eOt1ZMUGTWZ.js:143633:17)TypeError:无法在Object处读取null的属性'getData'。   (src / pages / dashboard / dashboard.spec.ts:47:35< - src / pages / das   hboard / dashboard.spec.js:40:31)Chrome 55.0.2883(Windows 8.1 0.0.0):   执行1 of 1(1 FAILED)ERROR(0.019秒/0.003秒

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { App, Config, Form, IonicModule, Keyboard, DomController, MenuController, NavController, Platform ,NavParams} from 'ionic-angular';
import { DashboardPage } from './dashboard';
import { ConfigMock } from '../../mock';
import { AzureDatasync } from '../../app/providers/azure-datasync';


let dashboard = null;
 let navCtrl: NavController; 
 let navParams: NavParams;
 let datasync: AzureDatasync;
 let fixture: ComponentFixture<DashboardPage> = null;
let instance: any = null;
describe('DashboardService', () => {

  //  beforeEach(() => {
 //     dashboard = new DashboardPage(navCtrl,navParams,datasync);
   //   console.log(dashboard);
   // });

      beforeEach(async(() => {

    TestBed.configureTestingModule({
      declarations: [DashboardPage],
      providers: [
        App, DomController, Form, Keyboard, MenuController, NavController, Platform,
       {provide: Config, useClass: ConfigMock},
      ],
      imports: [
        FormsModule,
        IonicModule,
        ReactiveFormsModule,
      ],
    })
    .compileComponents().then(() => {
      fixture = TestBed.createComponent(DashboardPage);
      console.log(fixture);

      instance = fixture;
      console.log(instance);
      fixture.detectChanges();
    });
  }));
    it('should return a non empty array', () => {

            let result = dashboard.getData();
             console.log(result);
            expect(Array.isArray(result)).toBeTruthy;
            expect(result.length).toBeGreaterThan(0);
        }
    );



});
import { Component, OnInit } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { AzureDatasync } from '../../app/providers/azure-datasync';
import 'zone.js/dist/async-test';

/*
  Generated class for the Dashboard page.

  See http://ionicframework.com/docs/v2/components/#navigation for more info on
  Ionic pages and navigation.
*/
@Component({
  selector: 'page-dashboard',
  templateUrl: 'dashboard.html'
})
export class DashboardPage implements OnInit {
  todays: any ;
  ticklers: any;
  cases: any;
  constructor(public navCtrl: NavController, public navParams: NavParams, public datasync: AzureDatasync) {}

  ngOnInit(){
    this.getData();
  }

  getData() {

    this.todays = [
        {type: "abc", name: "test", duration: "9.30 AM - 10.00 AM"},
        {type: "def", name: "test2", duration: "12.45 AM - 3.10 PM"}
    ];

    this.ticklers = [
        {name: "abc", description: "A Court "},
        {name: "def", description: "dd"}
    ];

    this.cases = [
        {name: "Msh", duedate: "Due on 7th Dec", priority: "PsI", timeduration: "24hrs", imgurl:"_blank.png"},
        {name: "ss Smith", duedate: "Due on 11th Dec", priority: "Pris", timeduration: "30 mins ", imgurl:"./person_blank.png"}
    ];
  }

}

1 个答案:

答案 0 :(得分:1)

我和Jasmine和Karma有同样的问题。

在我的karma.conf中,我将一些额外的文件加载到浏览器中:

// list of files / patterns to load in the browser
    files: [
      '../node_modules/zone.js/dist/zone.js', // 'Uncaught ReferenceError: Zone is not defined'
      '../node_modules/zone.js/dist/proxy.js', // 'TypeError: Cannot read property 'assertPresent' of undefined'
      '../node_modules/zone.js/dist/sync-test.js', // 'TypeError: Cannot read property 'assertPresent' of undefined'
      '../node_modules/zone.js/dist/jasmine-patch.js', // 'TypeError: Cannot read property 'assertPresent' of undefined'
      // ANY OTHER FILES TO LOAD FOR YOUR TESTS
    ],