Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Invoking suspend function #151

Closed
ychescale9 opened this issue Aug 27, 2019 · 3 comments · Fixed by #154
Closed

Support Invoking suspend function #151

ychescale9 opened this issue Aug 27, 2019 · 3 comments · Fixed by #154

Comments

@ychescale9
Copy link
Contributor

Right now to assert that invoking a suspend function throws an exception we have to do something like:

@Test
fun testASuspendFunctionThatThrowsTheExpectedException() {
    suspend fun func() {
        suspendCancellableCoroutine<Any> { throw IndexOutOfBoundsException() }
    }
    invoking {
        runBlocking {
            func()
        }
    } shouldThrow IndexOutOfBoundsException::class
}

Coroutines's runBlocking and runBlockingTest (from kotlinx-coroutines-test artifact) functions already provide a CoroutineScope as a lambda so it would be good to have a version of invoking that works with suspend functions:

@Test
fun testASuspendFunctionThatThrowsTheExpectedException() = runBlockingTest {
    suspend fun func() {
        suspendCancellableCoroutine<Any> { throw IndexOutOfBoundsException() }
    }
    invokingSuspend {
        func()
    } shouldThrow IndexOutOfBoundsException::class
}

I have this working locally and would love to send a PR if this makes sense.
Thanks!

@ychescale9
Copy link
Contributor Author

Implementation and tests.

@MarkusAmshove
Copy link
Owner

Hi @ychescale9 ,

this looks like a nice addition :-)

Feel free to send a PR!

@MarkusAmshove
Copy link
Owner

This is now implemented and released in v1.54 :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants