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 asserting suspend function #154

Merged
merged 4 commits into from
Sep 7, 2019

Conversation

ychescale9
Copy link
Contributor

Resolves #151.

This PR adds support for asserting suspend function invocation.

Sample usage:

@Test
fun testASuspendFunctionThatThrowsTheExpectedException() = runBlockingTest {
    suspend fun func() {
        suspendCancellableCoroutine<Any> { throw IndexOutOfBoundsException() }
    }
    invokingSuspend {
        func()
    } shouldThrow IndexOutOfBoundsException::class
}
  • invokingSuspend is the new API added that takes a suspend function as parameter.
  • runBlockingTest is an experimental API from kotlinx-coroutines-test which provides a CoroutineScope as a lambda. In this case runBlocking also works.
  • PR adds the kotlinx-coroutines-test dependency only for testing and no new runtime dependency is included in the library itself.

Checklist

  • I've added my name to the AUTHORS file, if it wasn't already present.

@ychescale9
Copy link
Contributor Author

@MarkusAmshove invokingSuspend sounds a bit verbose to me. MockK for example adds a co prefix to all the coroutine version of the APIs so in our case it would be coInvoking. But it's also a bit different since we don't really depend on any of the coroutines libraries. What do you think?

@MarkusAmshove
Copy link
Owner

Thanks for the PR and also updating the docs!
The implementation looks good and I'm willing to merge it, but since you asked about the naming I think I'm with you on the coInvoking.

I'm in favor of either invokeSuspending or coInvoking, since the ing form reads better with e.g. shouldThrow.

I'm fine with both, so I'll let you decide :-)

@ychescale9
Copy link
Contributor Author

Renamed to coInvoking 😃

@MarkusAmshove MarkusAmshove merged commit 0e103c5 into MarkusAmshove:master Sep 7, 2019
@ychescale9 ychescale9 deleted the yc/suspend-function branch September 7, 2019 13:21
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 this pull request may close these issues.

Support Invoking suspend function
2 participants