等待 Jasmine 的变量总是未定义
Variable always undefined with await Jasmine
我需要测试一个函数,在该函数中创建了一个变量并为其赋值。
但是有效负载常量始终未定义。
活动-card.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();
this.completed = payload.exists
}
分量-card.component.spec.ts
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
快到了。问题是这是一个可观察的对象,你不应该在其上模拟 pipe
函数,而是 return 一个来自函数调用的可观察对象。
试试这个:
import { of } from 'rxjs';
....
beforeEach(waitForAsync(() => {
fireStoreServiceMock.surveysUsers.and.returnValue({
doc: () => { return {
snapshotChanges: () => of({ exists: true }) // mock the observable with of
}}
}}
});
}));
我可能把括号 ({ }) 搞乱了,但应该可以了。
我需要测试一个函数,在该函数中创建了一个变量并为其赋值。
但是有效负载常量始终未定义。
活动-card.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();
this.completed = payload.exists
}
分量-card.component.spec.ts
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
快到了。问题是这是一个可观察的对象,你不应该在其上模拟 pipe
函数,而是 return 一个来自函数调用的可观察对象。
试试这个:
import { of } from 'rxjs';
....
beforeEach(waitForAsync(() => {
fireStoreServiceMock.surveysUsers.and.returnValue({
doc: () => { return {
snapshotChanges: () => of({ exists: true }) // mock the observable with of
}}
}}
});
}));
我可能把括号 ({ }) 搞乱了,但应该可以了。