与Firebase Firestore进行集成测试

时间:2020-05-16 23:36:17

标签: android kotlin google-cloud-firestore

我编写了一个简单的功能来从我的Firestore数据库中检索联赛,我将如何为此功能编写集成测试?该功能应该根据现有用户来抓取记录。

    override fun getPlayerLeague(): Observable<LeagueEntity> {
    return Observable.create { emitter ->

        auth.currentUser?.let { user ->
            val userLeagueQuery = database.collection(DATABASE_COLLECTION_PLAYERS).document(user.uid)
            userLeagueQuery.addSnapshotListener{ playerDoc, exception ->
                if (exception != null) {
                    return@addSnapshotListener
                }

                playerDoc?.let {
                    if (playerDoc.exists()) {
                        val foundLeague = playerDoc.data?.get("league") as? String
                        if (foundLeague != null) {
                            database.collection(DATABASE_COLLECTION_LEAGUES).document(foundLeague).addSnapshotListener{ leagueDoc, exception  ->
                                if (leagueDoc != null) {
                                    playerLeague = DataLeague (
                                        leagueDoc.id,
                                        leagueDoc.data?.get(DATABASE_FIELD_NAME) as? String,
                                        leagueDoc.data?.get(DATABASE_FIELD_DRAFT_STATUS) as? Long,
                                        leagueDoc.data?.get(DATABASE_FIELD_CREATOR) as? String,
                                        leagueDoc.data?.get(DATABASE_FIELD_PICKING_PLAYER) as? String,
                                        leagueDoc.data?.get(DATABASE_FIELD_PLAYER_LIST) as? ArrayList<HashMap<Any, Any>>,
                                        leagueDoc.data?.get(DATABASE_FIELD_SIZE) as? Long,
                                        leagueDoc.data?.get(DATABASE_FIELD_SNAKING_UP) as? Boolean
                                    )
                                    emitter.onNext(dataMapper.mapFrom(playerLeague!!))
                                }
                            }
                        } else {
                            emitter.onError(Throwable(""))
                        }
                    } else {
                        emitter.onError(Throwable(""))
                    }
                }
            }
        }
    }
}

0 个答案:

没有答案