angular - 如何测试返回承诺的链式方法?
问题描述
我有一个返回已解决承诺的方法。我遇到的问题是如何测试该承诺的解决方案。
基本上发生的事情是,我通过其他功能链更新我的文档,当该文档被保存时,我想显示一个警报。现在我无法测试我的分辨率。截至目前,警报从未被调用。
如果可能的话,我想使用本机承诺而不是其他库。我将如何测试事物以确保调用我的警报方法。
public update(doc):Promise<any> {
return collection.doc(doc.id).update(doc).then(res=>{
console.log('This is never run')
this.alert.insert({type:"success", message:"Updated"})
})
.catch(err=>{
console.log('Neither is this')
this.alert.insert({type:"error", message:`Error - Updating ${err}`})
})
}
// 我的测试
beforeEach(() => {
afSpy = jasmine.createSpyObj('AngularFirestore', ['collection',
'valueChanges', 'snapshotChanges', 'ref', 'doc','add','update',
'then', 'catch', 'finally', 'firestore', 'batch']);
afSpy.collection.and.returnValue(afSpy);
afSpy.valueChanges.and.returnValue(afSpy);
afSpy.snapshotChanges.and.returnValue(afSpy);
afSpy.ref.and.returnValue(afSpy);
afSpy.doc.and.returnValue(afSpy);
afSpy.add.and.returnValue(afSpy);
afSpy.update.and.returnValue(afSpy);
afSpy.then.and.returnValue(Promise.resolve('hello world'));
afSpy.catch.and.returnValue(afSpy);
afSpy.finally.and.callThrough()
afSpy.firestore.and.returnValue(afSpy);
afSpy.batch.and.returnValue(afSpy);
TestBed.configureTestingModule({
providers:[
{ provide: AngularFirestore, useValue: afSpy },
],
})
fit('temporary test', (done) => {
service.update(mockDoc).then(x=>{
expect(updateSpy).toHaveBeenCalledWith(mockDoc); //this passes
console.log(x)
expect(alertServiceSpy.insert).toHaveBeenCalled() // this fails
done();
})
});
解决方案
推荐阅读
- android - 从从firebase检索的图像视图中设置壁纸 - android
- logic - 创建一个对给定 x 值的多项式进行微分的函数?
- google-apps-script - 从自己的驱动器访问文件时如何使用更窄的 Google Apps 脚本授权范围
- assembly - 如何编码子立即 83 / 5 操作码的操作数,如 sub edx、0x3a?
- django - 如何将 Django 访问者文件上传到新的子目录,目录名称应该是会话名称?
- reactjs - 我们如何在扩展面板中跟踪每个表单的状态
- reactjs - Bing Maps V8 控件呈现空 div
- assembly - 为什么这不会改变内存中 array1 中某些元素的值?
- r - 将数据连接到 .kmz 或 .kml 并绘图
- android - Dagger - 如何在服务和片段之间共享提供的对象?