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

FsUnit does not work with Result

purkhusid opened this issue · comments

Description

FsUnit does not work with Result type

Repro steps

Please provide the steps required to reproduce the problem

type DomainError =
| ThisIsAnError of string

let doStuff a = if a = 0 then Error(DomainError.ThisIsAnError("yo")) else Ok("yeehaw")

[<Fact>]
let ``Testing stuff`` () =
    // Arrange
    let expectedError =
        Error(DomainError.ThisIsAnError("yeehaw"))

    // Act
    let result =
        doStuff 1337

    // Assert
    result |> should equal expectedError

Expected behavior

The test should pass

Actual behavior

Fails with

Expected: Equals Error (Unauthorized "yeehaw")
      Actual:   Error (Unauthorized "yeehaw")

Related information

Using FsUnit.Xunit 4.0.4

Hello @purkhusid,
when I run your code then I get this message:

FsUnit.Xunit+MatchException : Exception of type 'FsUnit.Xunit+MatchException' was thrown.
Expected: Equals Error (ThisIsAnError "yeehaw")
Actual:   Ok "yeehaw"

The expected Error doesn't match with the actual Ok.
I played with your example and I think you have to specify the type like that (which works):

[<Fact>]
let ``Testing stuff`` () =
    // Arrange
    let expectedError : Result<string, DomainError> =
        Error(DomainError.ThisIsAnError("yo"))

    // Act
    let result : Result<string, DomainError> =
        doStuff 0

    // Assert
    result |> should equal expectedError

In your example the compiler doesn't "know" your type in expectedError (Result<'a, DomainError>) see:
image
which have to be Result<string, DomainError>.

We already had that topic last year, see this post: #146 (comment)

I hope that helps. I'm hearing from you!

Hey! Thanks for taking your time responding to my issue! I figured that I just needed the type annotations, thanks again for taking your time!