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

[NUnit] Non provided generics is inferred to obj making test fails #274

Open
MangelMaxime opened this issue Apr 26, 2024 · 2 comments
Open

Comments

@MangelMaxime
Copy link

Description

I am testing an API which returns Result<'Success, 'Error>, my problem is that type inference is not fully propagating to the type making it resolved the unknown generic to obj instead of the correct type.

CleanShot 2024-04-26 at 17 02 15

This makes the tests fails, and the cause of the failure is not obvious when looking at the message:

A total of 1 test files matched the specified pattern.
  Failed fails [42 ms]
  Error Message:
     Assert.That(, )
  Expected: Ok { Type = "feat" } or Ok { Type = "feat" }
  But was:  Ok { Type = "feat" }

  Stack Trace:
     at FsUnit.TopLevelOperators.should[a,a](FSharpFunc`2 f, a x, Object actual)
   at Parser.validateFirstLine.fails() in /Users/mmangel/Workspaces/Github/easybuild-org/commit-linter/tests/Parser.fs:line 16

1)    at FsUnit.TopLevelOperators.should[a,a](FSharpFunc`2 f, a x, Object actual)
   at Parser.validateFirstLine.fails() in /Users/mmangel/Workspaces/Github/easybuild-org/commit-linter/tests/Parser.fs:line 16



Failed!  - Failed:     1, Passed:     0, Skipped:     0, Total:     1, Duration: 42 ms - EasyBuild.CommitLinter.Tests.dll (net8.0)

Repro steps

module Parser

open NUnit.Framework
open FsUnit
open CommitLinter

module validateFirstLine =

    type CommitMessage = { Type: string }

    let expectedTyped: Result<_, string> = { Type = "feat" } |> Ok

    [<Test>]
    let fails () =
        { Type = "feat" } |> Ok
        |> should equal expectedTyped

Expected behavior

The test should pass.

Actual behavior

Test is failing

Known workarounds

If you enforce the type then the test pass:

module validateFirstLine =

    type CommitMessage = { Type: string }

    let expectedTyped: Result<_, string> = { Type = "feat" } |> Ok

    [<Test>]
    let fails () =
        ({ Type = "feat" } |> Ok : Result<CommitMessage, string>) // See that here I am forcing the type resolution
        |> should equal expectedTyped

Related information

  • Operating system: OSX
  • Branch
  • .NET Runtime, CoreCLR or Mono Version: .NET 8
  • Performance information, links to performance testing scripts
@CaptnCodr
Copy link
Member

Hi @MangelMaxime,
sorry for the delay.

I have no solution for you. We already had those issues with type inference in previous issues ( #189 , #198 ).
I would have suggested what you already encountered and provided in "Known workaround".

Have a wonderful day.

@MangelMaxime
Copy link
Author

Hello @CaptnCodr,

If you think there is no solution or way to warn about it then we can close this issue.

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