Skip to content

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

@MangelMaxime

Description

@MangelMaxime

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions