Skip to content

Instantly share code, notes, and snippets.

View vkondrav's full-sized avatar
🎯
Focusing

Vitaliy Kondratiev vkondrav

🎯
Focusing
  • Toronto, Ontario
View GitHub Profile
@Test
fun `correct test`() = runTest {
val argumentCaptor = slot<BlackBoxSDK.Callback>()
val sdk = mockk<BlackBoxSDK> {
every { init(capture(argumentCaptor)) } returns Unit
}
CallbackToFlow(sdk).flow.test {
argumentCaptor.captured.callback("hello world")
//Terrible Refactor 1
class CallbackToFlow(sdk: BlackBoxSDK): BlackBoxSDK.Callback {
private val _flow = MutableSharedFlow<String>(extraBufferCapacity = 1)
val flow: Flow<String> = _flow
override fun callback(something: String) {
_flow.tryEmit(something)
}
@Test
fun `terrible test 1`() = runTest {
val context = RuntimeEnvironment.getApplication()
val sdk = BlackBoxSDK(context)
val subject = CallbackToFlow(sdk)
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
//TODO: location manager has to do something I think?
class CallbackToFlow(sdk: BlackBoxSDK) {
private val _flow = MutableSharedFlow<String>(extraBufferCapacity = 1)
val flow: Flow<String> = _flow
private val callback = object : BlackBoxSDK.Callback {
override fun callback(something: String) {
_flow.tryEmit(something)
}
}
interface BlackBoxSDK {
fun init(callback: Callback)
interface Callback {
fun callback(something: String)
}
}
@Test
fun `verify deleting favorite characters results in getIds flow responses`() = runTest {
val items = (0 until 3).map { id ->
FavoriteCharacter(
id = "id_$id",
name = "name_$id",
status = "status_$id",
species = "species_$id",
image = "image_$id",
)
@Test
fun `verify inserting favorite characters results in getAll flow responses`() = runTest {
val items = (0 until 3).map { id ->
FavoriteCharacter(
id = "id_$id",
name = "name_$id",
status = "status_$id",
species = "species_$id",
image = "image_$id",
)
@RunWith(RobolectricTestRunner::class)
class FavoriteCharacterDaoTest {
private val context: Context by lazy { RuntimeEnvironment.getApplication() }
private lateinit var subject: FavoriteCharactersDao
private lateinit var db: AppDatabase
@Before
fun setUp() {
@Database(
entities = [
FavoriteCharacter::class,
],
version = 1,
)
abstract class AppDatabase : RoomDatabase() {
abstract fun favoriteCharactersDao(): FavoriteCharactersDao
}
@Dao
interface FavoriteCharactersDao {
@Query("SELECT * FROM favorite_character")
fun getAll(): Flow<List<FavoriteCharacter>>
@Query("SELECT id FROM favorite_character")
fun getIds(): Flow<List<String>>
@Insert
suspend fun insert(favoriteCharacter: FavoriteCharacter)