Skip to content

Instantly share code, notes, and snippets.

Marco Cattaneo mcatta

Block or report user

Report or block mcatta

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@mcatta
mcatta / MainPresenterSpied.kt
Created Mar 28, 2020
An example to testing with method spyk
View MainPresenterSpied.kt
@Test
fun `Test log() method`() {
val spiedPresenter = spyk(mainPresenter)
every { dataRepository.fetchData() } returns listOf()
spiedPresenter.fetchData()
verify(exactly = 1) { spiedPresenter.log(any(), any()) }
}
@mcatta
mcatta / MockStatic.kt
Last active Mar 28, 2020
How mock a static method on a class
View MockStatic.kt
@Test
fun `test fetchData with a different behaviour of UUID generation`() {
val uuid = "my-fake-uuid"
mockkObject(MyUselessUtils)
every { MyUselessUtils.generateUUID() } returns uuid
every { dataRepository.fetchData() } returns listOf(DataModel(1, "Value"))
mainPresenter.fetchData()
val captureData = slot<List<UiDataModel>>()
@mcatta
mcatta / MainPresenter.kt
Created Mar 22, 2020
Mock static class
View MainPresenter.kt
class MainPresenter(
private val view: MainContract.View,
private val dataRepository: DataRepository
) : MainContract.Presenter {
override fun fetchData() {
try {
val result = dataRepository.fetchData()
view.onResult(result.map {
UiDataModel(
View EveryErrorRX.kt
every { dataRepository.fetchDataWithRX() } returns Single.error(Throwable("Oh oh something is broken"))
View EveryErrorRX.kt
every { dataRepository.fetchDataWithRX() } returns Single.error(Throwable("Oh oh something is broken"))
View MainPresenterTest.kt
@Test
fun `test fetchDataViaRX with an empty result`() {
every { dataRepository.fetchDataWithRX() } returns Single.just(listOf())
mainPresenter.fetchData()
val captureData = slot<List<UiDataModel>>()
verify(exactly = 1) { view.onResult(capture(captureData)) }
captureData.captured.let { res ->
assertNotNull(res)
@mcatta
mcatta / DataRepository.kt
Created Mar 22, 2020
Return an RX Java object
View DataRepository.kt
class DataRepository {
fun fetchDataWithRX(): Single<List<DataModel>> {
return Single.just(listOf(
DataModel(1, "Value 1"), DataModel(2, "Value 2"),
DataModel(3, "Value 3"), DataModel(4, "Value 4")
))
}
}
@mcatta
mcatta / MainPresenterRX.kt
Created Mar 22, 2020
The esample with RX
View MainPresenterRX.kt
package com.mcatta.mockksample.ui
import com.mcatta.mockksample.data.DataRepository
import com.mcatta.mockksample.domain.DataModel
import io.reactivex.Single
import io.reactivex.SingleObserver
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.util.*
View MainPresenterTestEx.kt
@Test
fun `Test fetchData with an exception`() {
every { dataRepository.fetchData() } throws IllegalStateException("Houston we have a problem")
mainPresenter.fetchData()
verify(exactly = 0) { view.onResult(any()) }
verify(exactly = 1) { view.onError(any()) }
}
@mcatta
mcatta / MainPresenterTest.kt
Last active Mar 22, 2020
Test fetchDataViaCallback()
View MainPresenterTest.kt
@Test
fun `test fetchDataViaCallback with an result`() {
val captureCallback = slot<(data: List<DataModel>) -> Unit>()
every { dataRepository.fetchData(capture(captureCallback)) } answers {
val fakeList = listOf(DataModel(1, "Value 1"))
captureCallback.captured.invoke(fakeList)
}
mainPresenter.fetchDataViaCallback()
val captureData = slot<List<UiDataModel>>()
You can’t perform that action at this time.