Angular 如何测试返回承诺的链式方法?
我有一个方法返回一个已解决的承诺。我遇到的问题是如何测试承诺的解决方案 基本上,我通过一系列其他函数更新文档&保存文档时,我希望显示一个警报。现在我无法测试我的决心。到目前为止,警报从未被调用 我想使用原生承诺&如果可能的话,不要使用其他库。我将如何进行测试,以确保调用我的警报方法Angular 如何测试返回承诺的链式方法?,angular,jasmine,angular-test,Angular,Jasmine,Angular Test,我有一个方法返回一个已解决的承诺。我遇到的问题是如何测试承诺的解决方案 基本上,我通过一系列其他函数更新文档&保存文档时,我希望显示一个警报。现在我无法测试我的决心。到目前为止,警报从未被调用 我想使用原生承诺&如果可能的话,不要使用其他库。我将如何进行测试,以确保调用我的警报方法 public update(doc):Promise<any> { return collection.doc(doc.id).update(doc).then(res=>{ c
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();
})
});