fsprojects / FsUnit

FsUnit makes unit-testing with F# more enjoyable. It adds a special syntax to your favorite .NET testing framework.

Home Page:http://fsprojects.github.io/FsUnit/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using `should throw` does not show the raised exception, only the string version of the lambda

abelbraaksma opened this issue · comments

Description

With FsUnit.XUnit using should throw, the report of "Expected vs Actual" shows the string version of the lambda function that was used to pass into the test.

Another issue is that, when the result type of the function-under-test is not unit, the test will always fail with a similar vague message.

Repro steps

You can repro with this test:

// XUnit test
[<Fact>]
let ``Test raising exception`` () = task {
    fun () -> failwith "help" |> ignore
    |> should throw typeof<HttpRequestException>  // should fail

Expected behavior

Something like this is expected, which would clearly indicate what goes wrong.

FsUnit.Xunit+MatchException : Exception of type 'FsUnit.Xunit+MatchException' was thrown.
Expected: System.Net.Http.HttpRequestException
Actual:   System.Exception

Actual behavior

But this is shown instead, which suggests there wasn't an exception at all and the type is wrong.

FsUnit.Xunit+MatchException : Exception of type 'FsUnit.Xunit+MatchException' was thrown.
Expected: System.Net.Http.HttpRequestException
Actual:   <fun:Pipe #1 input at line 112@112>

Known workarounds

None that I found.

Related information

  • Operating system: Win11
  • Branch: latest from Nuget
  • Using .NET 6.0
  • Using FsUnit.Xunit

Is it even possible to stringify lambdas?
Only the compiler knows the types here, does he?

@CaptnCodr yeah, but since this is throwing, the throw helper should just catch whatever comes and report that back. The point here is that Actual contains the lambda, but should contain the result of executing that lambda (i.e.: either an exception or not an exception). The lambda itself is not the "actual" value here.

@abelbraaksma Yes, I see. It should be similar to the NUnit output, right:

Message: 
  Expected: System.Net.Http.HttpRequestException
  But was:  System.Exception: help
   at <StartupCode$FsUnit-NUnit-Test>.$ShouldFailTests.Pipe #1 input at line 33@33.Invoke(Unit unitVar0) in C:\Users\const\github\repos\FsUnit\tests\FsUnit.NUnit.Test\shouldFailTests.fs:line 33
   at FsUnit.TopLevelOperators.y@37.Invoke()
--- End of stack trace from previous location ---
   at NUnit.Framework.Internal.ExceptionHelper.Rethrow(Exception exception)
   at NUnit.Framework.Internal.Reflect.DynamicInvokeWithTransparentExceptions(Delegate delegate)
   at NUnit.Framework.Internal.ExceptionHelper.RecordException(Delegate parameterlessDelegate, String parameterName)

I will analyse this.

Hey @abelbraaksma,
a fix (#216) has been released in v5.0.4 and is already available at Nuget. 🙂

Great stuff, tx!