I am trying to assert that an exception was thrown. Here is a cut down piece of code that reproduces the problem:
open FsUnit
open Xunit
let testException () =
raise <| Exception()
[<Fact>]
let ``should assert throw correctly``() =
(testException ())
|> should throw typeof<System.Exception>
The error says that a System.Exception was thrown but the test should pass as that is what I am asserting. Can someone please help with where I am going wrong.
You're calling the
testExceptionfunction, and then passing its result as argument to theshouldfunction. At runtime,testExceptioncrashes, and so never returns a result, and so theshouldfunction is never called.If you want the
shouldfunction to catch and correctly report the exception, you need to pass it thetestExceptionfunction itself, not its result (for there is no result in the first place). This way, theshouldfunction will be able to invoketestExceptionwithin atry..withblock and thus catch the exception.