我正在学习全栈开发,我刚刚完成了我的第一个react js + firestore项目。我想写一些单元测试,但我在互联网上找到的唯一示例是测试firestore规则或测试云函数。例如,我想测试我的查询是否返回期望值。
export async function doesUsernameExist(username) {
const result = await firebase
.firestore()
.collection('users')
.where('username', '==', username)
.get();
return result.docs.map((user) => user.data().length > 0);
}我想用一个假数据测试这个查询,但是我怎么才能做一个假数据呢?更准确地说,我需要一个可以查询的数据结构。提前谢谢你!
发布于 2021-04-10 20:27:04
我解决了我的问题。我用的是firebase模拟器。在我的例子中,解决方案是:
而不是
const firebase = Firebase.initializeApp(config);使用此配置
const firebase = require('@firebase/testing')
const db = firebase.initializeTestApp({ projectId: MY_PRIJECT_ID }).firestore()
async function doesUsernameExist(username) {
const result = await db
.collection('users')
.where('username', '==', username)
.get();
return result.docs.map((user) => user.data().length > 0);
}
describe('Testing username', () => {
it('Username does not exsitst in the database', async () => {
const username = "peter"
const res = await doesUsernameExist(username)
expect(res.length).toEqual(0)
})
})https://stackoverflow.com/questions/67010562
复制相似问题