-
Notifications
You must be signed in to change notification settings - Fork 0
createProfileActivity: mock Authenticator #269
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,8 +11,10 @@ import androidx.test.espresso.matcher.ViewMatchers.withId | |
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import ch.sdp.vibester.R | ||
import ch.sdp.vibester.TestMode | ||
import ch.sdp.vibester.auth.FireBaseAuthenticator | ||
import ch.sdp.vibester.database.DataGetter | ||
import ch.sdp.vibester.user.User | ||
import com.google.firebase.auth.FirebaseUser | ||
import dagger.hilt.android.testing.BindValue | ||
import dagger.hilt.android.testing.HiltAndroidRule | ||
import dagger.hilt.android.testing.HiltAndroidTest | ||
|
@@ -39,21 +41,36 @@ class CreateProfileActivityTest { | |
} | ||
|
||
@BindValue @JvmField | ||
val mockUsersRepo = mockk<DataGetter>() | ||
val mockAuthenticator = mockk<FireBaseAuthenticator>() | ||
|
||
private fun createMockInvocation(email: String) { | ||
every {mockUsersRepo.createUser(any(), any(), any(), any())} answers { | ||
lastArg<(String) -> Unit>().invoke(email) | ||
} | ||
private fun createMockAuthenticator() { | ||
val mockUser = createMockUser() | ||
every { mockAuthenticator.getCurrUser() } returns mockUser | ||
every { mockAuthenticator.getCurrUID() } returns mockUser.uid | ||
} | ||
|
||
every { mockUsersRepo.getUserData(any(), any()) } answers { | ||
secondArg<(User) -> Unit>().invoke(User()) | ||
} | ||
private fun createMockUser(): FirebaseUser { | ||
val email = "[email protected]" | ||
val uid = "mockuseruid" | ||
val mockUser = mockk<FirebaseUser>() | ||
every { mockUser.email } returns email | ||
every { mockUser.uid } returns uid | ||
return mockUser | ||
} | ||
|
||
every { mockUsersRepo.getCurrentUser() } answers { | ||
null | ||
} | ||
@BindValue @JvmField | ||
val mockDataGetter = mockk<DataGetter>() | ||
|
||
private fun createMockDataGetter(email: String) { | ||
every {mockDataGetter.createUser(any(), any(), any(), any())} answers { | ||
lastArg<(String) -> Unit>().invoke(email) | ||
} | ||
every { mockDataGetter.getUserData(any(), any()) } answers {secondArg<(User) -> Unit>().invoke(User())} | ||
every { mockDataGetter.getCurrentUser() } answers {null } | ||
every { mockDataGetter.setSubFieldValue(any(), any(), any(), any()) } answers {} | ||
every { mockDataGetter.updateFieldInt(any(), any(), any(), any()) } answers {} | ||
every { mockDataGetter.setFieldValue(any(), any(), any()) } answers {} | ||
every { mockDataGetter.updateSubFieldInt(any(), any(), any(), any(), any()) } answers {} | ||
} | ||
|
||
@After | ||
|
@@ -69,9 +86,9 @@ class CreateProfileActivityTest { | |
|
||
val intent = Intent(ApplicationProvider.getApplicationContext(), CreateProfileActivity::class.java) | ||
intent.putExtra("email", mockEmail) | ||
intent.putExtra("isUnitTest", true) | ||
|
||
createMockInvocation(mockEmail) | ||
createMockDataGetter(mockEmail) | ||
createMockAuthenticator() | ||
|
||
val scn: ActivityScenario<CreateProfileActivity> = ActivityScenario.launch(intent) | ||
|
||
|
@@ -83,7 +100,6 @@ class CreateProfileActivityTest { | |
|
||
Intents.intended(IntentMatchers.hasComponent(ProfileActivity::class.java.name)) | ||
Intents.intended(IntentMatchers.hasExtra("email", mockEmail)) | ||
Intents.intended(IntentMatchers.hasExtra("isUnitTest", true)) | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -84,6 +84,17 @@ class FireBaseAuthenticator @Inject constructor() { | |||||||||||||||||||||
return Firebase.auth.currentUser | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
/** | ||||||||||||||||||||||
* Getter for the current user ID | ||||||||||||||||||||||
*/ | ||||||||||||||||||||||
fun getCurrUID(): String { | ||||||||||||||||||||||
var uid = "" | ||||||||||||||||||||||
if (isLoggedIn()) { | ||||||||||||||||||||||
uid = FirebaseAuth.getInstance().currentUser!!.uid | ||||||||||||||||||||||
} | ||||||||||||||||||||||
return uid | ||||||||||||||||||||||
} | ||||||||||||||||||||||
Comment on lines
+87
to
+96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
See 40 lines above !!! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, but when you mock, you cant access one from the above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will mock others as well, and remove those from companion object. sounds good? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ohh okay I see, yes maybe removing the companion object may be a good idea because I find it weird otherwise to have two times the same code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is... I decided to remove one by one, so it easier to check! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will remove these function on the next pr since there a lot of dependencies. My next pr (where these repetetive functions are removed) is almost done! |
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
/** | ||||||||||||||||||||||
* A function to log in with email and password | ||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nooooo bye my friend isUnitTest, you had a great life 😂😂😂😂 !!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ahahahahahhahaa bye-bye and never come back please