Created
June 20, 2022 12:10
-
-
Save micHar/8262eafe78df54b02ca86f634fc950b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface IssueReporter { | |
fun reportIssue(e: Exception) | |
} | |
interface SoundPlayer { | |
suspend fun playSound() | |
} | |
class MediaPlayer( | |
private val scope: CoroutineScope, | |
private val soundPlayer: SoundPlayer, | |
private val issueReporter: IssueReporter | |
) { | |
val playerErrors = MutableSharedFlow<Exception>() | |
init { | |
scope.launch { | |
playerErrors.collect { | |
issueReporter.reportIssue(it) | |
} | |
} | |
} | |
fun play() { | |
scope.launch { | |
try { | |
soundPlayer.playSound() | |
} catch (e: Exception) { | |
playerErrors.emit(e) | |
} | |
} | |
} | |
} | |
@OptIn(ExperimentalCoroutinesApi::class) | |
class NeverEndingCoroutineTest { | |
@Test | |
fun `should emit error when playSound throws`() = runTest { | |
val exception = Exception("Oopsie") | |
val soundPlayer = mockk<SoundPlayer>() | |
val issueReporter = mockk<IssueReporter>() | |
justRun { issueReporter.reportIssue(any()) } | |
coEvery { soundPlayer.playSound() } throws exception | |
val sut = MediaPlayer(this, soundPlayer, issueReporter) | |
sut.playerErrors.test { | |
sut.play() | |
awaitItem() shouldBe exception | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment