我是这样模拟AngularFirestore的:
beforeEach(() => {
TestBed.configureTestingModule({
{
providers: [
{
provide: AngularFirestore,
useValue: {
collection: () => ({
doc: () => ({
set: () => new Promise((resolve) => resolve(void 0))})
})
}
}
}
]
}
})
})
collection()
返回一个包含doc
属性的对象。collection().doc()
返回一个包含set
属性的函数。collection().doc().set()
返回void Promise
。
如何使用spyOn
监视对set
的调用?
spyOn
允许您监视一个对象所拥有的一个属性。你必须:
- 自己构建间谍目标
- 基于#1构建Jasmine间谍对象
- 嵌入#1到模拟的Firestore
:
let firestoreSpy: jasmine.Spy;
beforeEach(() => {
const firestoreGet = {
set: () => new Promise((resolve) => resolve(void 0))
};
firestoreSpy = spyOn(firestoreGet, 'set');
TestBed.configureTestingModule({
{
providers: [
{
provide: AngularFirestore,
useValue: {
collection: () => ({
doc: () => firestoreGet
}
}
}
]
}
})
})
it('mister it', () => expect(firestoreSpy.[what have you]).[to be whatever]);