Skip to content

Commit

Permalink
✅Add SignupViewModelTest
Browse files Browse the repository at this point in the history
  • Loading branch information
89645321 committed Nov 3, 2023
1 parent 5b8f21f commit eed3370
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 49 deletions.
2 changes: 1 addition & 1 deletion frontend/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ android {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}

}

dependencies {
Expand Down Expand Up @@ -123,6 +124,5 @@ dependencies {
testImplementation ("io.mockk:mockk-android:$mockkVersion")
testImplementation ("io.mockk:mockk-agent:$mockkVersion")
testImplementation ("org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutinesVersion")
testImplementation ("junit:junit:4.13")
testImplementation ("androidx.arch.core:core-testing:2.1.0")
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,19 @@ import io.mockk.coEvery
import io.mockk.impl.annotations.MockK
import io.mockk.mockk
import junit.framework.TestCase.assertEquals
import kotlinx.coroutines.flow.flowOf
import org.junit.Before
import org.junit.Test
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.launch
import kotlinx.coroutines.newSingleThreadContext
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runBlockingTest
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.junit.Test

//@RunWith(JUnit4::class)
class SignupViewModelTest {
/*
private val mainThreadSurrogate = newSingleThreadContext("UI thread")

@MockK
Expand All @@ -41,26 +33,27 @@ class SignupViewModelTest {
private val validNickname = "valid_nickname_"
private val longNickname = "invalid_nickname"
private val emptyNickname = ""

// boundary condition: 8 characters in password field
private val validPassword = "password"
private val shortPassword = "pwd"
private val validEmail = "[email protected]"
private val emptyString = ""

@get:Rule
val instantExecutorRule = InstantTaskExecutorRule()

@OptIn(ExperimentalCoroutinesApi::class)
@Before
@Rule
fun setup() {
//Dispatchers.setMain(mainThreadSurrogate)
Dispatchers.setMain(mainThreadSurrogate)
viewModel = SignupViewModel(repository)
//val instantTaskExecutorRule = InstantTaskExecutorRule()
}

@After
fun tearDown() {
//Dispatchers.resetMain()
//mainThreadSurrogate.close()
Dispatchers.resetMain()
mainThreadSurrogate.close()
}

@Test
Expand Down Expand Up @@ -91,7 +84,7 @@ class SignupViewModelTest {
}

@Test
fun signupViewModel_setNickname_empty_afterSignupClick(){
fun signupViewModel_setNickname_empty_afterSignupClick() {
viewModel.setNickname(emptyNickname)

viewModel.signup(validEmail)
Expand All @@ -108,7 +101,7 @@ class SignupViewModelTest {
}

@Test
fun signupViewModel_setNickname_long_afterSignupClick(){
fun signupViewModel_setNickname_long_afterSignupClick() {
viewModel.setNickname(longNickname)

viewModel.signup(validEmail)
Expand All @@ -125,7 +118,7 @@ class SignupViewModelTest {
}

@Test
fun signupViewModel_setNickname_valid_afterSignupClick(){
fun signupViewModel_setNickname_valid_afterSignupClick() {
viewModel.setNickname(validNickname)

viewModel.signup("[email protected]")
Expand All @@ -142,7 +135,7 @@ class SignupViewModelTest {
}

@Test
fun signupViewModel_setPassword_short_beforeSignupClick(){
fun signupViewModel_setPassword_short_beforeSignupClick() {
viewModel.setPassword(shortPassword)

assertEquals(shortPassword, viewModel.passwordInput)
Expand All @@ -151,7 +144,7 @@ class SignupViewModelTest {
}

@Test
fun signupViewModel_setPassword_valid_beforeSignupClick(){
fun signupViewModel_setPassword_valid_beforeSignupClick() {
viewModel.setPassword(validPassword)

assertEquals(validPassword, viewModel.passwordInput)
Expand All @@ -160,7 +153,7 @@ class SignupViewModelTest {
}

@Test
fun signupViewModel_setPassword_short_afterSignupClick(){
fun signupViewModel_setPassword_short_afterSignupClick() {
viewModel.setPassword(shortPassword)
viewModel.setNickname(validNickname)

Expand All @@ -178,7 +171,7 @@ class SignupViewModelTest {
}

@Test
fun signupViewModel_setPassword_valid_afterSignupClick(){
fun signupViewModel_setPassword_valid_afterSignupClick() {
viewModel.setPassword(validPassword)

viewModel.signup(validEmail)
Expand All @@ -196,7 +189,7 @@ class SignupViewModelTest {


@Test
fun signupViewModel_setPasswordCheck_invalid_beforeSignupClick(){
fun signupViewModel_setPasswordCheck_invalid_beforeSignupClick() {
viewModel.setPassword(validPassword)
viewModel.setPasswordCheck(shortPassword)

Expand All @@ -206,7 +199,7 @@ class SignupViewModelTest {
}

@Test
fun signupViewModel_setPasswordCheck_valid_beforeSignupClick(){
fun signupViewModel_setPasswordCheck_valid_beforeSignupClick() {
viewModel.setPassword(validPassword)
viewModel.setPasswordCheck(validPassword)

Expand All @@ -216,7 +209,7 @@ class SignupViewModelTest {
}

@Test
fun signupViewModel_setPasswordCheck_invalid_afterSignupClick(){
fun signupViewModel_setPasswordCheck_invalid_afterSignupClick() {
viewModel.setPassword(validPassword)
viewModel.setNickname(validNickname)
viewModel.setPasswordCheck(shortPassword)
Expand All @@ -235,7 +228,7 @@ class SignupViewModelTest {
}

@Test
fun signupViewModel_setPasswordCheck_valid_afterSignupClick(){
fun signupViewModel_setPasswordCheck_valid_afterSignupClick() {
viewModel.setPassword(validPassword)
viewModel.setPasswordCheck(validPassword)

Expand All @@ -253,7 +246,7 @@ class SignupViewModelTest {
}

@Test
fun signupViewModel_signup_emptyNickname_shortPwd_invalidPwdCheck(){
fun signupViewModel_signup_emptyNickname_shortPwd_invalidPwdCheck() {
viewModel.setNickname(emptyNickname)
viewModel.setPassword(shortPassword)
viewModel.setPasswordCheck(validPassword)
Expand All @@ -268,7 +261,7 @@ class SignupViewModelTest {
}

@Test
fun signupViewModel_signup_emptyNickname_shortPwd_validPwdCheck(){
fun signupViewModel_signup_emptyNickname_shortPwd_validPwdCheck() {
viewModel.setNickname(emptyNickname)
viewModel.setPassword(shortPassword)
viewModel.setPasswordCheck(shortPassword)
Expand All @@ -283,7 +276,7 @@ class SignupViewModelTest {
}

@Test
fun signupViewModel_signup_emptyNickname_validPwd_invalidPwdCheck(){
fun signupViewModel_signup_emptyNickname_validPwd_invalidPwdCheck() {
viewModel.setNickname(emptyNickname)
viewModel.setPassword(validPassword)
viewModel.setPasswordCheck(shortPassword)
Expand All @@ -298,7 +291,7 @@ class SignupViewModelTest {
}

@Test
fun signupViewModel_signup_emptyNickname_validPwd_validPwdCheck(){
fun signupViewModel_signup_emptyNickname_validPwd_validPwdCheck() {
viewModel.setNickname(emptyNickname)
viewModel.setPassword(validPassword)
viewModel.setPasswordCheck(validPassword)
Expand All @@ -313,7 +306,7 @@ class SignupViewModelTest {
}

@Test
fun signupViewModel_signup_longNickname_shortPwd_invalidPwdCheck(){
fun signupViewModel_signup_longNickname_shortPwd_invalidPwdCheck() {
viewModel.setNickname(longNickname)
viewModel.setPassword(shortPassword)
viewModel.setPasswordCheck(validPassword)
Expand All @@ -328,7 +321,7 @@ class SignupViewModelTest {
}

@Test
fun signupViewModel_signup_longNickname_shortPwd_validPwdCheck(){
fun signupViewModel_signup_longNickname_shortPwd_validPwdCheck() {
viewModel.setNickname(longNickname)
viewModel.setPassword(shortPassword)
viewModel.setPasswordCheck(shortPassword)
Expand All @@ -343,7 +336,7 @@ class SignupViewModelTest {
}

@Test
fun signupViewModel_signup_longNickname_validPwd_invalidPwdCheck(){
fun signupViewModel_signup_longNickname_validPwd_invalidPwdCheck() {
viewModel.setNickname(longNickname)
viewModel.setPassword(validPassword)
viewModel.setPasswordCheck(shortPassword)
Expand All @@ -358,7 +351,7 @@ class SignupViewModelTest {
}

@Test
fun signupViewModel_signup_longNickname_validPwd_validPwdCheck(){
fun signupViewModel_signup_longNickname_validPwd_validPwdCheck() {
viewModel.setNickname(longNickname)
viewModel.setPassword(validPassword)
viewModel.setPasswordCheck(validPassword)
Expand All @@ -373,7 +366,7 @@ class SignupViewModelTest {
}

@Test
fun signupViewModel_signup_validNickname_shortPwd_invalidPwdCheck(){
fun signupViewModel_signup_validNickname_shortPwd_invalidPwdCheck() {
viewModel.setNickname(validNickname)
viewModel.setPassword(shortPassword)
viewModel.setPasswordCheck(validPassword)
Expand All @@ -388,7 +381,7 @@ class SignupViewModelTest {
}

@Test
fun signupViewModel_signup_validNickname_shortPwd_validPwdCheck(){
fun signupViewModel_signup_validNickname_shortPwd_validPwdCheck() {
viewModel.setNickname(validNickname)
viewModel.setPassword(shortPassword)
viewModel.setPasswordCheck(shortPassword)
Expand All @@ -403,7 +396,7 @@ class SignupViewModelTest {
}

@Test
fun signupViewModel_signup_validNickname_validPwd_invalidPwdCheck(){
fun signupViewModel_signup_validNickname_validPwd_invalidPwdCheck() {
viewModel.setNickname(validNickname)
viewModel.setPassword(validPassword)
viewModel.setPasswordCheck(shortPassword)
Expand All @@ -418,23 +411,35 @@ class SignupViewModelTest {
}

@Test
@OptIn(ExperimentalCoroutinesApi::class)
fun signupViewModel_signup_validNickname_validPwd_validPwdCheck() = runTest{
fun signupViewModel_signup_validNickname_validPwd_validPwdCheck() = runTest {
val authSignupRequest = AuthSignupRequest(validEmail, validPassword, validNickname)
launch(Dispatchers.Main){
coEvery { repository.signup(authSignupRequest) } returns flowOf(Resource.success(null))
coEvery { repository.signup(authSignupRequest) } returns flowOf(Resource.success(null))

viewModel.setNickname(validNickname)
viewModel.setPassword(validPassword)
viewModel.setPasswordCheck(validPassword)
viewModel.setNickname(validNickname)
viewModel.setPassword(validPassword)
viewModel.setPasswordCheck(validPassword)

viewModel.signup(validEmail)
}
viewModel.signup(validEmail)

assertEquals(viewModel.signupResult.value?.message, null)
assertEquals(viewModel.signupResult.value?.data, null)

}
*/

@Test
fun signupViewModel_clearInputs() {
viewModel.setNickname(validNickname)
viewModel.setPassword(validPassword)
viewModel.setPasswordCheck(validPassword)

assertEquals(validNickname, viewModel.nicknameInput)
assertEquals(validPassword, viewModel.passwordInput)
assertEquals(validPassword, viewModel.passwordCheckInput)

viewModel.clearInputs()

assertEquals(emptyString, viewModel.nicknameInput)
assertEquals(emptyString, viewModel.passwordInput)
assertEquals(emptyString, viewModel.passwordCheckInput)
}
}

0 comments on commit eed3370

Please sign in to comment.