Angular 变量始终未定义为wait Jasmine
我需要测试一个函数,在该函数中创建一个变量并为其赋值 但有效载荷常数始终未定义 活动卡.component.tsAngular 变量始终未定义为wait Jasmine,angular,typescript,jasmine,karma-jasmine,Angular,Typescript,Jasmine,Karma Jasmine,我需要测试一个函数,在该函数中创建一个变量并为其赋值 但有效载荷常数始终未定义 活动卡.component.ts async ngOnInit() { const { uid } = await this.auth.currentUser const { payload } = await this.firestore.surveysUsers(this.survey.id).doc(uid).snapshotChanges().pipe(first()).toPromise()
async ngOnInit() {
const { uid } = await this.auth.currentUser
const { payload } = await this.firestore.surveysUsers(this.survey.id).doc(uid).snapshotChanges().pipe(first()).toPromise();
this.completed = payload.exists
}
beforeEach(waitForAsync(() => {
fireStoreServiceMock.surveysUsers.and.returnValue({
doc: () => { return {
snapshotChanges: () => { return {
pipe: () => { return {
toPromise: () => { return { exists: true }}
}}
}}
}}
});
}));
describe('tests in delete', () => {
it('should update surveys', fakeAsync(() => {
fixture.detectChanges();
component.delete().then(() => {
});
fixture.detectChanges();
tick();
expect(fireStoreServiceMock.surveys).toHaveBeenCalled();
expect(snackBarMock.open).toHaveBeenCalled();
}));
});
组件卡.组件规范ts
async ngOnInit() {
const { uid } = await this.auth.currentUser
const { payload } = await this.firestore.surveysUsers(this.survey.id).doc(uid).snapshotChanges().pipe(first()).toPromise();
this.completed = payload.exists
}
beforeEach(waitForAsync(() => {
fireStoreServiceMock.surveysUsers.and.returnValue({
doc: () => { return {
snapshotChanges: () => { return {
pipe: () => { return {
toPromise: () => { return { exists: true }}
}}
}}
}}
});
}));
describe('tests in delete', () => {
it('should update surveys', fakeAsync(() => {
fixture.detectChanges();
component.delete().then(() => {
});
fixture.detectChanges();
tick();
expect(fireStoreServiceMock.surveys).toHaveBeenCalled();
expect(snackBarMock.open).toHaveBeenCalled();
}));
});
错误
Error: Uncaught (in promise): TypeError: Cannot read property 'exists' of undefined
TypeError: Cannot read property 'exists' of undefined
at CampaignsCardComponent.<anonymous> (http://localhost:9876/_karma_webpack_/webpack:/src/app/modules/campaigns/pages/campaigns-card/campaigns-card.component.ts:43:4)
at Generator.next (<anonymous>)
at fulfilled (http://localhost:9876/_karma_webpack_/webpack:/node_modules/tslib/tslib.es6.js:71:42)
at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-evergreen.js:372:1)
at ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/proxy.js:126:1)
at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-evergreen.js:371:1)
at Object.onInvoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2015/core.js:28535:1)
at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-evergreen.js:371:1)
at Zone.run (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-evergreen.js:134:1)
at http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-evergreen.js:1276:1
错误:未捕获(承诺中):TypeError:无法读取未定义的属性“exists”
TypeError:无法读取未定义的属性“exists”
在活动斯卡德组件。(http://localhost:9876/_karma_webpack_/webpack:/src/app/modules/campaigns/pages/campaigns-卡片/活动卡片.组件ts:43:4)
在Generator.next()处
至少(http://localhost:9876/_karma_webpack_/webpack:/node_modules/tslib/tslib.es6.js:71:42)
在ZoneDelegate.invoke(http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-evergreen.js:372:1)
在Proxyzonspect.onInvoke(http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/proxy.js:126:1)
在ZoneDelegate.invoke(http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-evergreen.js:371:1)
在Object.onInvoke(http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm2015/core.js:28535:1)
在ZoneDelegate.invoke(http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-evergreen.js:371:1)
在Zone.run(http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-常青树.js:134:1)
在http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-evergreen.js:1276:1
差不多了。问题是这是一个可观察的,您不应该在其上模拟管道
函数,而是从函数调用返回一个可观察的
试试这个:
import { of } from 'rxjs';
....
beforeEach(waitForAsync(() => {
fireStoreServiceMock.surveysUsers.and.returnValue({
doc: () => { return {
snapshotChanges: () => of({ exists: true }) // mock the observable with of
}}
}}
});
}));
我可能把括号({})和更改弄乱了,但这应该可以做到。我做了这个更改,但问题仍然是一样的。我设法解决了这个问题,因为它是一个常量,我需要返回一个内部存在的有效负载对象。