Skip to content

Instantly share code, notes, and snippets.

🚀
Building Coinverse's Android app

AdamHurwitz AdamSHurwitz

🚀
Building Coinverse's Android app
Block or report user

Report or block AdamSHurwitz

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
@AdamSHurwitz
AdamSHurwitz / CoinverseDatabase.kt
Created Dec 26, 2019
UDF + Unit Tests - Database
View CoinverseDatabase.kt
object CoinverseDatabase {
lateinit var database: CoinverseDatabaseBuilder
fun init(context: Context) {
database = CoinverseDatabaseBuilder.getAppDatabase(context)
}
}
@AdamSHurwitz
AdamSHurwitz / FeedLoadContentTests.kt
Last active Dec 26, 2019
UDF + Unit Tests - Verify Tests
View FeedLoadContentTests.kt
@ParameterizedTest
@MethodSource("FeedLoad")
fun `Feed Load`(test: FeedLoadTest) = testDispatcher.runBlockingTest {
mockComponents(test)
FeedLoad(test.feedType, ...).also { event ->
viewModel.feedLoad(event)
assertContentList(test, event)
}
verifyTests(test)
}
@AdamSHurwitz
AdamSHurwitz / TestUtils.kt
Last active Dec 26, 2019
UDF + Unit Tests - Test Utils
View TestUtils.kt
fun ContentViewModel.feedViewState() = this.feedViewState.getOrAwaitValue()
fun ContentViewModel.viewEffects() = this.viewEffect.getOrAwaitValue()
fun <T> LiveData<Event<T>>.observe() = this.getOrAwaitValue().peekEvent()
/**
* Gets the value of a [LiveData] or waits for it to have one, with a timeout.
*
* Use this extension from host-side (JVM) tests. It's recommended to use it alongside
* `InstantTaskExecutorRule` or a similar mechanism to execute tasks synchronously.
*/
@AdamSHurwitz
AdamSHurwitz / ContentViewModel.kt
Last active Dec 25, 2019
UDF + Unit Tests - ViewModel
View ContentViewModel.kt
class ContentViewModel : ViewModel(), ContentViewEvents {
val feedViewState: LiveData<FeedViewState> get() = _feedViewState
val viewEffect: LiveData<ContentEffects> get() = _viewEffect
private val _feedViewState = MutableLiveData<FeedViewState>()
private val _viewEffect = MutableLiveData<ContentEffects>()
override fun feedLoad(event: FeedLoad) {
_feedViewState.value = FeedViewState(
feedType = event.feedType,
@AdamSHurwitz
AdamSHurwitz / ContentViewEventType.kt
Created Dec 25, 2019
UDF + Unit Tests - View Event
View ContentViewEventType.kt
sealed class ContentViewEventType {
data class FeedLoad(val feedType: FeedType, val timeframe: Timeframe, val isRealtime: Boolean) : ContentViewEventType()
data class FeedLoadComplete(val hasContent: Boolean) : ContentViewEventType()
...
}
@AdamSHurwitz
AdamSHurwitz / FeedLoadContentTests.kt
Last active Dec 26, 2019
UDF + Unit Tests - Assert View State
View FeedLoadContentTests.kt
@ParameterizedTest
@MethodSource("FeedLoad")
fun `Feed Load`(test: FeedLoadTest) = testDispatcher.runBlockingTest {
mockComponents(test)
FeedLoad(test.feedType, ...).also { event ->
viewModel.feedLoad(event)
// Test view state and effects with AssertJ.
assertContentList(test, event)
}
verifyTests(test)
@AdamSHurwitz
AdamSHurwitz / FeedLoadContentTests.kt
Last active Dec 25, 2019
UDF + Unit Tests - Init View Events
View FeedLoadContentTests.kt
@ParameterizedTest
@MethodSource("FeedLoad")
fun `Feed Load`(test: FeedLoadTest) = testDispatcher.runBlockingTest {
mockComponents(test)
// Create view event with Sealed data class.
FeedLoad(test.feedType, ...).also { event ->
viewModel.feedLoad(event)
assertContentList(test, event)
}
verifyTests(test)
@AdamSHurwitz
AdamSHurwitz / FeedLoadContentTests.kt
Last active Dec 25, 2019
UDF + Unit Tests - Constant Mocks
View FeedLoadContentTests.kt
// Mock constant values.
mockkStatic("app.coinverse.utils.ConstantsKt")
...
every {
NETWORK_ERROR
} returns "Mock network error"
@AdamSHurwitz
AdamSHurwitz / FeedLoadContentTests.kt
Last active Dec 25, 2019
UDF + Unit Tests - Test Static
View FeedLoadContentTests.kt
// Mock static methods from Android libraries.
mockkStatic(FirebaseRemoteConfig::class)
...
every {
FirebaseRemoteConfig.getInstance()
} returns mockk(relaxed = true)
@AdamSHurwitz
AdamSHurwitz / Constants.kt
Last active Dec 25, 2019
UDF + Unit Tests - Test Constant Value
View Constants.kt
// Top-level constant values
// Package: 'app.coinverse.utils.Constants.kt'
val NETWORK_ERROR = "Some error message."
You can’t perform that action at this time.