我有一个像下面这样的模拟服务
const firebaseService = jest.fn(() => ({
initializeApp: jest.fn(() => { /*do nothing*/}),
}))
在我的测试中,我想expect
是否initializeApp
已被调用。我该如何检查?
it('should be called', () => {
expect(???).toHaveBeenCalledTimes(1);
});
更新:真实场景
const collection = jest.fn(() => {
return {
doc: jest.fn(() => {
return {
collection: collection,
update: jest.fn(() => Promise.resolve(true)),
onSnapshot: jest.fn(() => Promise.resolve(true)),
get: jest.fn(() => Promise.resolve(true))
}
}),
where: jest.fn(() => {
return {
get: jest.fn(() => Promise.resolve(true)),
onSnapshot: jest.fn(() => Promise.resolve(true)),
limit: jest.fn(() => {
return {
onSnapshot: jest.fn(() => Promise.resolve(true)),
get: jest.fn(() => Promise.resolve(true)),
}
}),
}
}),
limit: jest.fn(() => {
return {
onSnapshot: jest.fn(() => Promise.resolve(true)),
get: jest.fn(() => Promise.resolve(true)),
}
})
}
});
const Firestore = {
collection: collection
}
firebaseService = {
initializeApp() {
// do nothing
},
firestore: Firestore
};
我想在下面检查
expect(firebaseService.firestore.collection).toHaveBeenCalled();
expect(firebaseService.firestore.collection.where).toHaveBeenCalled();
expect(firebaseService.firestore.collection.where).toHaveBeenCalledWith(`assignedNumbers.123`, '==', true);