ngonInit()的Angular 2单元测试函数

时间:2019-07-09 16:24:45

标签: javascript angular unit-testing jasmine ionic3

我有一个离子应用程序,需要为此页面编写单元测试。我是茉莉的新手。如何为以下方法编写测试用例

import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';

  import { MarketingPreference } from '../../models/marketing-preference';
  import { PreferencesService } from  '../../providers/authentication/preferences.service';
  import { CommonConstants } from '../../providers/common.constants';
  import { UtilService } from './../../providers/util.service';

@Component({
selector: 'marketing-preferences-content',
templateUrl: 'marketing-preferences-content.html'
})
export class MarketingPreferencesContent implements OnInit {
listPreferences: MarketingPreference[];

@Input() loginEnrollmentFlow: boolean;
@Output() preferencesSubmittedSuccess: EventEmitter<any> = new EventEmitter();
@Output() preferencesSubmittedError: EventEmitter<any> = new EventEmitter();

constructor(private preferencesService: PreferencesService, private utilService: UtilService) {}

ngOnInit(): void {
    this.preferencesService
        .getPreferences('marketing')
        .then(({ preferences }) => {
            this.listPreferences = preferences;
        })
        .catch(() => {
            this.utilService.showAlert('', CommonConstants.ErrorMessages.SERVER_DOWN, CommonConstants.Buttons.OK);
        });
}

checkBoxListener(e: HTMLInputElement, index: number): void {
    this.listPreferences[index].election = e.checked ? CommonConstants.Application_Variables.YES : CommonConstants.Application_Variables.NO;
}

updatePreferences(): void {
    this.preferencesService
        .postPreferences({ preferences: this.listPreferences })
        .then(() => {
            this.preferencesSubmittedSuccess.emit(this.listPreferences);
        })
        .catch(error => {
            this.preferencesSubmittedError.emit(error);
        });
}
 }

我的说明

import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { UtilService } from '../../providers/util.service';
import { MarketingPreferencesContent } from './marketing-preferences-content';
 import { PreferencesService } from 
   '../../providers/authentication/preferences.service';
import { } 'jasmine';
import { CommonConstants } from '../../providers/common.constants';
import { MarketingPreference } from '../../models/marketing-preference';

describe('MarketingPreferencesContent', () => {
let fixture: ComponentFixture<MarketingPreferencesContent>;
let component: MarketingPreferencesContent;
let mockUtilService: UtilService;
let mockPreferencesService: PreferencesService;

beforeEach(async(() => {
    mockUtilService = jasmine.createSpyObj('utils', ['showAlert']);
    mockPreferencesService = jasmine.createSpyObj('preferencesService', ['getPreferences', 'postPreferences']);

    TestBed.configureTestingModule({
        declarations: [MarketingPreferencesContent],
        schemas: [CUSTOM_ELEMENTS_SCHEMA],
        providers: [
            { provide: UtilService, useValue: mockUtilService },
            { provide: PreferencesService, useValue: mockPreferencesService }
        ]
    });

    fixture = TestBed.createComponent(MarketingPreferencesContent);
    component = fixture.componentInstance;
}));

it('should create the component instance', async(() => {
    expect(component).toBeDefined();
}));

});

0 个答案:

没有答案