Oll*_*ohn 6 javascript unit-testing karma-runner karma-jasmine angular
我正在尝试对一个组件进行单元测试,该组件具有使用declare关键字声明的变量,并且它有几个方法,然后在组件中稍后调用,如下所示:
import {
Component,
OnInit
} from '@angular/core';
import {
AlertController,
App,
Events,
Platform
} from "ionic-angular";
declare
let PushNotification: any;
@Component({
selector: 'push',
template: ``
})
export class PushComponent implements OnInit {
nav: any;
_push: any;
constructor() {}
ngOnInit() {
// ...
this._push = PushNotification.init({
android: {
senderID: '...'
},
ios: {
alert: "true",
badge: false,
sound: 'false'
},
windows: {}
});
/**
* On Registration, we want to register the device with BE
*/
this._push.on('registration', (data) => {
// ...
});
/**
* If we have a notification come through we need to work out what we are doing with it
*/
this._push.on('notification', (notification) => {
// ...
});
}
/**
* Based on the payload we need to perform different actions
* @param payload
*/
public pushAction(payload: any) {
// ...
}
/**
* The user has received a notification while using the app so we need to show an alert
* @param notification
*/
public pushAlert(notification: any) {
// ...
}
/**
* Reset the badge number to 0
*/
public resetBadge() {
// ...
}
/**
* Register deviceId and platform
* @param registrationId
*/
public registerPush(registrationId) {
// ...
}
/**
* De-Register the deviceId
* @param deviceId
*/
public deRegisterPush(deviceId) {
// ...
}
}Run Code Online (Sandbox Code Playgroud)
在我的单元测试中,我希望能够断言init()和on()方法已被调用,但我总是收到一个错误,说PushNotification未定义,所以我希望有人能够告诉我如何访问或注入它以某种方式进入组件?
我的测试文件如下:
// Core files and modules
import {
TestBed,
ComponentFixture
} from '@angular/core/testing';
import {
NO_ERRORS_SCHEMA
} from '@angular/core';
import {
App,
AlertController,
Events,
Platform,
Config
} from 'ionic-angular';
import {
ViewController,
Slides
} from 'ionic-angular';
import {
mockOpportunityProvider,
mockPushProvider,
mockAuth,
mockAppPlatform,
mockPlatform,
mockEvents,
presentableControllerMock
} from '../../mocks';
import {
OpportunityProvider
} from '../../providers/opportunity/opportunity.provider';
import {
AuthProvider
} from '../../providers/auth/auth';
import {
ProfileComponent
} from '../../pages/profile/profile.component';
import {
PushProvider
} from '../../providers/push/push.provider';
// Pages
import {
PushComponent
} from './push.component';
import {
OpportunityComponent
} from '../../pages/opportunity/opportunity.component';
let component: PushComponent;
let fixture: ComponentFixture < PushComponent > ;
describe('Component: ProfileTemplateComponent', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [],
declarations: [PushComponent],
providers: [{
provide: OpportunityProvider,
useClass: mockOpportunityProvider
},
{
provide: AuthProvider,
useClass: mockAuth
},
{
provide: PushProvider,
useClass: mockPushProvider
},
{
provide: App,
useClass: mockAppPlatform
},
ProfileComponent,
{
provide: Platform,
useClass: mockPlatform
},
Config,
{
provide: AlertController,
useClass: presentableControllerMock
},
{
provide: Events,
useClass: mockEvents
}
],
schemas: [NO_ERRORS_SCHEMA]
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(PushComponent);
component = fixture.componentInstance;
});
afterEach(() => {
fixture.destroy();
component = null;
});
// TODO - figure out how to access the PushNotification variable
it('expect ngOnInit() to call ', async() => {
// ...
});
it('expect pushAction() to call nav.setRoot()', () => {
// ...
});
it('expect pushAlert() to call alertCtrl.create() and present()', () => {
// ...
});
it('expect resetBadge() to call _push.setApplicationIconBadgeNumber() if on cordova platform', () => {
// ...
});
it('expect registerPush() to call pushProvider.register()', () => {
// ...
});
it('expect deRegisterPush() to call pushProvider.deRegister()', () => {
// ...
});
});Run Code Online (Sandbox Code Playgroud)
提前致谢!
| 归档时间: |
|
| 查看次数: |
973 次 |
| 最近记录: |