Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FsUnit does not work with Result #189

Closed
purkhusid opened this issue Sep 27, 2021 · 2 comments
Closed

FsUnit does not work with Result #189

purkhusid opened this issue Sep 27, 2021 · 2 comments

Comments

@purkhusid
Copy link

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

@CaptnCodr
Copy link
Member

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!

@purkhusid
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants