question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 5 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
xhanincommented, Aug 12, 2018

I think the problem is that the compiler can’t infer a ()->Unit type for the function when expect is called, while with your third example it can infer it directly for throws.

Saying that I found another workaround:

expect<()->Unit> {"blah"}.throws<java.lang.RuntimeException>()

And yet another one:

fun expecting(subject: ()->Unit): DescribeableBuilder<()->Unit> =
        expect(subject)

expecting {"blah"}.throws<java.lang.RuntimeException>()
0reactions
christophsturmcommented, Aug 12, 2018

i submitted a youtrack issue for this https://youtrack.jetbrains.com/issue/KT-26063

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found