expect {...}.throws works only when the block returns unit
See original GitHub issue expect {}.throws<java.lang.RuntimeException>() // this works
expect {"blah"}.throws<java.lang.RuntimeException>() // this is red in idea
throws<RuntimeException> {"blah"} // this works
this is a really strange problem, the kotlin compiler seems to be fine with a ()-> Unit block returning something in line 3 but in line 2 it complains
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (6 by maintainers)
Top Results From Across the Web
mockk every {}.throws() Exception fails test - Stack Overflow
I think you can only expect that certain exception is thrown. It's no point to test language basic behaviour: if something thrown -...
Read more >How to test a function that's expected to throw error in jest…
So, I needed to write unit tests for a function that's expected to throw an error if the parameter supplied is undefined and...
Read more >Exceptions | Kotlin
All exception classes in Kotlin inherit the Throwable class. Every exception has a message, a stack trace, and an optional cause.
Read more >How to write a unit test which passes if a function throws?
I want to define a function which returns a value if successful (typically, if the arguments are ... "Tested block did not throw...
Read more >Unit Testing Exceptions in C# - Chad Golden
In this article I will work through examples of how to unit test C# code that's expected to throw exceptions.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think the problem is that the compiler can’t infer a
()->Unit
type for the function whenexpect
is called, while with your third example it can infer it directly forthrows
.Saying that I found another workaround:
And yet another one:
i submitted a youtrack issue for this https://youtrack.jetbrains.com/issue/KT-26063