Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 离子2单元测试:“;未捕获类型错误:无法读取属性'_getPortal';“未定义的投掷”;_Angular_Ionic2_Karma Jasmine - Fatal编程技术网

Angular 离子2单元测试:“;未捕获类型错误:无法读取属性'_getPortal';“未定义的投掷”;

Angular 离子2单元测试:“;未捕获类型错误:无法读取属性'_getPortal';“未定义的投掷”;,angular,ionic2,karma-jasmine,Angular,Ionic2,Karma Jasmine,我是离子2单元测试的初学者。我陷入了一个错误,无法解决: “未捕获的TypeError:无法读取未定义抛出的属性“\u getPortal” 这是我的ts文件: //Angular Module Import import { Component, OnInit } from '@angular/core'; import { NavController, NavParams } from 'ionic-angular'; import { ConfigService } from '../..

我是离子2单元测试的初学者。我陷入了一个错误,无法解决: “未捕获的TypeError:无法读取未定义抛出的属性“\u getPortal”

这是我的ts文件:

//Angular Module Import
import { Component, OnInit } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { ConfigService } from '../../../providers/config.service';
import { Http, Response, RequestOptions, Headers } from '@angular/http';
import { ViewController } from 'ionic-angular';

//App Services Imports
import { Globals } from '../../../providers/globals';
import { GetPaymentusService } from '../../../providers/getPaymentusUrl.service';

//App Component Imports
import { PolicyDocumentPage } from '../document/policydocument';

@Component({
    selector: 'page-watercraft-policy',
    templateUrl: 'watercraftpolicy.html'
})
export class WaterCraftPolicyPage implements OnInit {

    watercraftPolicyData: any;
    index: number;
    customerName: any;

    constructor(public navCtrl: NavController, 
                public navParams: NavParams, 
                public global: Globals, 
                public http: Http,
                public _config: ConfigService, 
                public paymentusService: GetPaymentusService,
                public viewCtrl: ViewController) {
    }

    ionViewDidLoad() {
        this.viewCtrl.setBackButtonText('My Policy');
    }

    ngOnInit() {
        this.watercraftPolicyData = this.navParams.get('param1');
        this.index = this.navParams.get('param2');
        this.customerName = this.navParams.get('param3');
        this.watercraftPolicyData.csppolicyNickName = "WATERCRAFT";
    }

    policyDocumentPage() {
        this.navCtrl.push(PolicyDocumentPage, {
            param1: this.watercraftPolicyData,
            param2: this.index,
            param3: this.customerName
        });
    }

    myBillsPage() {
        let text: string;
        this.paymentusService.getPolicy(this.watercraftPolicyData)
            .map((policyRequest) => {
                 if (policyRequest.header.error.errorCode === '0001'){
                    this._config.showError();
                } else if(policyRequest.header.status === 'UNAUTHORIZED'){
                             this._config.showUnathorisedError();
                        }else if(policyRequest.header.error.errorCode === '1001'){
                             this._config.showUnexpectedError();
                         }else if(policyRequest.status === '403' ||
                                 policyRequest.header.status === 'FORBIDDEN'){
                             this._config.showSessionTimeOutError();
                      }else{
                this.global.payBill(policyRequest.customerPayment.paymentusURL);
            }
            }).subscribe(response => { },
                    error => {
                        this._config.showError();
                    });



    }

}
单元测试文件:

  import { TestBed, ComponentFixture, async } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import { IonicModule } from 'ionic-angular';
import { NavController,NavParams} from 'ionic-angular';
import { ViewController } from 'ionic-angular';
import { AlertController } from 'ionic-angular';
import { File } from '@ionic-native/file';
import { FileOpener } from '@ionic-native/file-opener';
import { PdfViewerPage } from '../viewer/pdfviewer';

import { HanoverApp } from '../../../app/app';
import { WaterCraftPolicyPage } from './watercraftpolicy';

import { ViewControllerMock } from '../../../mocks';
import { MockNavParams } from '../../../mocks';
import { NavMock } from '../../../mocks';
import { MockAlertController } from '../../../mocks';

import { Globals } from '../../../providers/globals';
import { GetPaymentusService } from '../../../providers/getPaymentusUrl.service';
import { GetIdCardService } from '../../../providers/getIdCard.service';
import { ConfigService } from '../../../providers/config.service';
import { Http, Response, RequestOptions, Headers } from '@angular/http';


let comp: WaterCraftPolicyPage;
let fixture: ComponentFixture<WaterCraftPolicyPage>;
let de: DebugElement;
let el: HTMLElement;



describe('Page: WaterCraftPolicyPage', () => {

    beforeEach(async(() => {

        TestBed.configureTestingModule({

            declarations: [HanoverApp, WaterCraftPolicyPage],

            providers: [NavController,{provide: NavParams, useClass: MockNavParams},
            {provide: ViewController, useClass: ViewControllerMock},
            Globals,GetPaymentusService,GetIdCardService,ConfigService],

            imports: [
                IonicModule.forRoot(HanoverApp)
            ]

        }).compileComponents();

    }));

    beforeEach(() => {

        fixture = TestBed.createComponent(WaterCraftPolicyPage);
        comp    = fixture.componentInstance;

    });

    afterEach(() => {
        fixture.destroy();
        comp = null;
        de = null;
        el = null;
    });

    it('is created', () => {
        expect(fixture).toBeTruthy();
        expect(comp).toBeTruthy();
    });

});

您错过了在提供程序中设置NavMock…我也尝试过,但仍然得到相同的错误我尝试过:提供程序:[{provide:NavController,useClass:NavMock},{provide:NavParams,useClass:MockNavParams},{provide:ViewController,useClass:ViewControllerMock},Globals,GetPaymentusService,GetIdCardService,ConfigService],可能不在
IonicModule中使用
forRoot
不,它不起作用您错过了在provider中设置NavMock…我也尝试过,但仍然得到相同的错误我尝试过:providers:[{provider:NavController,useClass:NavMock},{provide:NavParams,useClass:MockNavParams},{provide:ViewController,useClass:ViewControllerMock},Globals,GetPaymentusService,GetIdCardService,ConfigService],可能不在
IonicModule
中使用
forRoot
,它不工作
export class NavMock {

  public pop(): any {
    return new Promise(function(resolve: Function): void {
      resolve();
    });
  }

  public push(): any {
    return new Promise(function(resolve: Function): void {
      resolve();
    });
  }

  public getActive(): any {
    return {
      'instance': {
        'model': 'something',
      },
    };
  }

  public setRoot(): any {
    return true;
  }
}

export class ViewControllerMock {

    public readReady: any = {
        emit(): void {

        },
        subscribe(): any {

        }
    };

    public writeReady: any = {
        emit(): void {

        },
        subscribe(): any {

        }
    };

    public contentRef(): any {
        return new Promise(function (resolve: Function): void {
            resolve();
        });
    }

    public didEnter(): any {
        return new Promise(function (resolve: Function): void {
            resolve();
        });
    }

    public didLeave(): any {
        return new Promise(function (resolve: Function): void {
            resolve();
        });
    }

    public onDidDismiss(): any {
        return new Promise(function (resolve: Function): void {
            resolve();
        });
    }

    public onWillDismiss(): any {
        return new Promise(function (resolve: Function): void {
            resolve();
        });
    }

    public willEnter(): any {
        return new Promise(function (resolve: Function): void {
            resolve();
        });
    }

    public willLeave(): any {
        return new Promise(function (resolve: Function): void {
            resolve();
        });
    }

    public willUnload(): any {
        return new Promise(function (resolve: Function): void {
            resolve();
        });
    }

    public dismiss(): any {
        return true;
    }

    public enableBack(): any {
        return true;
    }

    public getContent(): any {
        return true;
    }

    public hasNavbar(): any {
        return true;
    }

    public index(): any {
        return true;
    }

    public isFirst(): any {
        return true;
    }

    public isLast(): any {
        return true;
    }

    public pageRef(): any {
        return true;
    }

    public setBackButtonText(): any {
        return true;
    }

    public showBackButton(): any {
        return true;
    }

    public _setHeader(): any {
        return true;
    }

    public _setIONContent(): any {
        return true;
    }

    public _setIONContentRef(): any {
        return true;
    }

    public _setNavbar(): any {
        return true;
    }

    public _setContent(): any {
        return true;
    }

    public _setContentRef(): any {
        return true;
    }

    public _setFooter(): any {
        return true;
    }

}


export class MockNavParams{

   public get(param): any{
    return {'csppolicyNickName':'data'};
  }
}



export class MockAlertController{
 create(): any{
   return true;
 };

}