angular - 我如何在茉莉花中模拟 AngularFirestore 链式方法?
问题描述
我如何模拟 angularfirestore 并测试placeOrder方法?
constructor(private afs: AngularFirestore) {}
placeOrder(order: Order, restaurantId: string): Observable<void> {
return from(this.afs.doc<Order(`restaurants/${restaurantId}/orders/${order.id}`)
.set(order, { merge: true }));
}
解决方案
可能是这样的:
const AngularFirestoreStub = {
doc: () => {
return {
set: () => {
// I assume it is a promise because of `from`
return Promise.resolve(/*... Data you want here */),
}
};
}
};
....
let afs: any; // declare afs
....
beforeEach(
async(() => {
TestBed.configureTestingModule({
imports: [ RouterTestingModule],
providers: [{provide: AngularFirestore, useValue: AngularFirestoreStub}]
declarations: [ AppComponent ]
}).compileComponents();
})
);
afs = TestBed.get(AngularFirestore); // if using Angular 9, this should be .inject instead of .get
it('calls doc with the correct route', async done => {
// spy on calls to the fireStore.doc and call its actual implementation
spyOn(afs, 'doc').and.callThrough();
// call the method and await its response by converting the observable to a promise
await component.placeOrder({ id: 1 }, 1).pipe(take(1)).toPromise();
// expect fireStore.doc was called with the right argument
expect(afs, 'doc').toHaveBeenCalledWith('restaurants/1/orders/1');
done();
});
推荐阅读
- javascript - 如何将旧的(非事件驱动的)软件移植到 javascript
- ios - swift中使用mvvm模式的网络调用函数
- c++ - 无法通过引用函数“calcValues”中的变量“firstNum”和“secondNum”来更改值
- java - 如何解决时区和 SSL 的问题
- android - 我可以使用 NDK 访问设备文件(/dev/...)吗?
- java - Java - 无法导入 JIprolog 包
- php - Xdebug:如何禁用“xdebug.halt_level”
- node.js - 无法在 ember 中创建组件,每次都会遇到错误
- javascript - 如何从 jquery 对话框表中删除表的行?
- reactjs - 有条件地在 React 组件中渲染