forked from Droidcon-Boston/conference-app-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a helper class for mocking firebase data for Droidcon-Boston#138.
- Loading branch information
Showing
5 changed files
with
92 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...ston/app/src/test/java/com/mentalmachines/droidcon_boston/firebase/FirebaseHelperRobot.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.mentalmachines.droidcon_boston.firebase | ||
|
||
import com.google.firebase.database.DataSnapshot | ||
import com.google.firebase.database.DatabaseReference | ||
import com.google.firebase.database.ValueEventListener | ||
import com.mentalmachines.droidcon_boston.data.FirebaseDatabase | ||
import org.mockito.ArgumentMatchers.anyString | ||
import org.mockito.Mockito.* | ||
|
||
/** | ||
* A robot class that can be used to extract out firebase functionality from the tests. | ||
* | ||
* Mainly we can use this to extract mocking. | ||
*/ | ||
class FirebaseHelperRobot(mockFirebaseHelper: FirebaseHelper) { | ||
private val mockSpeakerReference = mock(DatabaseReference::class.java) | ||
|
||
init { | ||
`when`(mockFirebaseHelper.speakerDatabase).thenReturn(mockSpeakerReference) | ||
`when`(mockSpeakerReference.orderByChild(anyString())).thenReturn(mockSpeakerReference) | ||
} | ||
|
||
fun mockSpeakers(speakers: List<FirebaseDatabase.EventSpeaker>): FirebaseHelperRobot { | ||
val speakerSnapshots = speakers.map { speaker -> | ||
val mockSnapshot = mock(DataSnapshot::class.java) | ||
`when`(mockSnapshot.getValue(FirebaseDatabase.EventSpeaker::class.java)).thenReturn( | ||
speaker | ||
) | ||
return@map mockSnapshot | ||
} | ||
|
||
val mockSnapshot = mock(DataSnapshot::class.java) | ||
`when`(mockSnapshot.children).thenReturn(speakerSnapshots) | ||
|
||
doAnswer { | ||
val valueEventListener = it.arguments.first() as ValueEventListener | ||
valueEventListener.onDataChange(mockSnapshot) | ||
return@doAnswer null | ||
}.`when`(mockSpeakerReference).addValueEventListener(any(ValueEventListener::class.java)) | ||
|
||
return this | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters