Skip to content

Instantly share code, notes, and snippets.

@IlyaGulya
Created September 2, 2018 06:25
Show Gist options
  • Save IlyaGulya/d9f7962a7d38deced2cc9e0cd188db96 to your computer and use it in GitHub Desktop.
Save IlyaGulya/d9f7962a7d38deced2cc9e0cd188db96 to your computer and use it in GitHub Desktop.
Phone auth test example
interface PhoneAuth {
fun sendSms(phone: String): Completable
fun login(code: String): Completable
fun changePhone()
fun getPhoneAuthState(): Observable<PhoneAuthState>
fun getProgressState(): Observable<ProgressState>
fun getErrorState(): Observable<ErrorState>
companion object {
const val DEFAULT_TIMER = 60L
}
sealed class PhoneAuthState {
data class WaitingCode(
val timeRemaining: Long
) : PhoneAuthState()
object InputPhone : PhoneAuthState()
object TimeExpired : PhoneAuthState()
}
sealed class ErrorState {
object NoErrors : ErrorState()
data class Error(
val error: Throwable
) : ErrorState()
}
sealed class ProgressState {
object Visible : ProgressState()
object Gone : ProgressState()
}
}
class PhoneAuthImplTest {
val loginInteractor: LoginInteractor = mock()
val phoneAuthService: PhoneAuthService = mock()
lateinit var phoneAuth: PhoneAuth
var progressObserver = TestObserver.create<PhoneAuth.ProgressState>()
var errorObserver = TestObserver.create<PhoneAuth.ErrorState>()
var phoneAuthStateObserver = TestObserver.create<PhoneAuth.PhoneAuthState>()
val disposable = CompositeDisposable().apply {
add(progressObserver)
add(errorObserver)
add(phoneAuthStateObserver)
}
@Before
fun setUp() {
phoneAuth = PhoneAuthImpl(loginInteractor, phoneAuthService)
phoneAuth.getProgressState().subscribe(progressObserver)
phoneAuth.getErrorState().subscribe(errorObserver)
phoneAuth.getPhoneAuthState().subscribe(phoneAuthStateObserver)
}
@After
fun tearDown() {
disposable.clear()
}
@Test
fun `sendSms must show progress bar and hide it after successful request`() {
val scheduler = TestScheduler()
whenever(phoneAuthService.sendSms(any())).thenReturn(Completable.timer(1, TimeUnit.SECONDS, scheduler))
phoneAuth.sendSms("+79999999999").subscribe()
progressObserver.assertValues(PhoneAuth.ProgressState.Gone, PhoneAuth.ProgressState.Visible)
scheduler.advanceTimeBy(1, TimeUnit.SECONDS)
progressObserver.assertValues(PhoneAuth.ProgressState.Gone, PhoneAuth.ProgressState.Visible, PhoneAuth.ProgressState.Gone)
}
}
class PhoneAuthImplTest {
val loginInteractor: LoginInteractor = mock()
val phoneAuthService: PhoneAuthService = mock()
lateinit var phoneAuth: PhoneAuth
var progressSubscriber = TestObserver.create<PhoneAuth.ProgressState>()
var errorSubscriber = TestObserver.create<PhoneAuth.ErrorState>()
var phoneAuthStateSubscriber = TestObserver.create<PhoneAuth.PhoneAuthState>()
val disposable = CompositeDisposable().apply {
add(progressSubscriber)
add(errorSubscriber)
add(phoneAuthStateSubscriber)
}
@Before
fun setUp() {
phoneAuth = PhoneAuthImpl(loginInteractor, phoneAuthService)
phoneAuth.getProgressState().subscribe(progressSubscriber)
phoneAuth.getErrorState().subscribe(errorSubscriber)
phoneAuth.getPhoneAuthState().subscribe(phoneAuthStateSubscriber)
}
@After
fun tearDown() {
disposable.clear()
}
@Test
fun `sendSms must show progress bar and hide it after successful request`() {
val scheduler = TestScheduler()
whenever(phoneAuthService.sendSms(any())).thenReturn(Completable.timer(1, TimeUnit.SECONDS, scheduler))
phoneAuth.sendSms("+79994522411").subscribe()
progressSubscriber.assertValues(PhoneAuth.ProgressState.Gone, PhoneAuth.ProgressState.Visible)
scheduler.advanceTimeBy(1, TimeUnit.SECONDS)
progressSubscriber.assertValues(PhoneAuth.ProgressState.Gone, PhoneAuth.ProgressState.Visible, PhoneAuth.ProgressState.Gone)
}
}
interface PhoneAuthService {
fun sendSms(phone: String): Completable
fun sendCode(code: String): Single<Observable<Response<AuthorizeAnswer>>>
fun getTimerDuration(): Long
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment