Using 'should throw' or 'should throwWithMessage' never succeeds unless function returns unit
See original GitHub issueDescription
This is related to #214, but not the same.
The following test always fails:
[<Fact>]
let ``Test raising exception`` () =
fun () -> raise (ArgumentException "help")
|> should (throwWithMessage "help") typeof<ArgumentException>
Repro steps
See above.
Expected behavior
Test should succeed.
Actual behavior
Test fails with the following message:
Message:
FsUnit.Xunit+MatchException : Exception of type 'FsUnit.Xunit+MatchException' was thrown.
Expected: System.ArgumentException "help"
Actual: <fun:Test raising exception@127>
Known workarounds
The following fixes the test, but this is not obvious. I should note that in the FsUnit help pages, all examples include ignore
, but this isn’t obvious and it took me a while to figure out what was wrong with my test (after our discussion in #214.
[<Fact>]
let ``Test raising exception`` () =
fun () -> raise (ArgumentException "help") |> ignore
|> should (throwWithMessage "help") typeof<ArgumentException>
Related information
There may be a reason why ignore
is required in such tests. But if there’s some way that this isn’t required, I think that would be awesome: mainly because it isn’t trivial to figure this out once it hits you.
PS: great work on fixing and maintaining this lib! It’s generally a breeze to use!
Issue Analytics
- State:
- Created a year ago
- Comments:15 (15 by maintainers)
Top Results From Across the Web
should function returns include "never" if they can throw an ...
If I have a function that under normal circumstances returns a value, but can throw an exception what should the prototype look like?...
Read more >What is FsUnit?
FsUnit is a set of libraries that makes unit-testing with F# more enjoyable. ... A function should throw a certain type of exception:...
Read more >Function returning true/false vs. void when succeeding and ...
Therefore, deciding between throw and return requires other criteria. Throwing exceptions should often be avoided if it endangers the efficiency ...
Read more >Exceptions
So throwing an ApplicationException when an Exception was expected will not fail the assertion. However, if you really want to be explicit about...
Read more >How to know if a function throws in Typescript?
Never is intended for functions that always throw, when you union never with another type it just vanishes. Upvote 1
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
exactly, but…
this didn’t work
Awesome, tx!
Hey @abelbraaksma, thanks for the little repro, I’ll look at it this week. 🙂