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.

Using 'should throw' or 'should throwWithMessage' never succeeds unless function returns unit

See original GitHub issue

Description

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:closed
  • Created a year ago
  • Comments:15 (15 by maintainers)

github_iconTop GitHub Comments

1reaction
abelbraaksmacommented, Dec 22, 2022

you don’t need ignore anymore as in your opening post:

exactly, but…

Also on functions that return obj or a generic type 'a. That is valid for throwWithMessage.

this didn’t work

tested it with throw without ignore, that test failed for xUnit. I’ll put it on my list (https://github.com/fsprojects/FsUnit/issues/225).

Awesome, tx!

1reaction
CaptnCodrcommented, Dec 19, 2022

Hey @abelbraaksma, thanks for the little repro, I’ll look at it this week. 🙂

Read more comments on GitHub >

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

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