Skip to content

[Bug]: Specified method is not supported exception when using IsEquivalentTo on two structs that implement IEquatable #5040

Description

@roy-t

Description

I am using TUnit 1.17.54 on .NET 10

I have the following class that implements IEquatable

public sealed class FilePath : IEquatable<FilePath>
{
    private readonly string Path;
    // Equals(object), Equals(FilePath), GetHashCode, == and != are properly overloaded
}

When I test if two arrays have the same contents:

    [Test]
    public async Task MinimalExampleTest()
    {
        var openedFile = new FilePath(@"D:\test_a.tmp");        
        var createdFile = new FilePath(@"D:\test_c.tmp");
        FilePath[] actual = [openedFile, createdFile];
        FilePath[] expected = [openedFile, createdFile];

        await Assert.That(actual).IsEquivalentTo(expected, CollectionOrdering.Any);
    }

The test throws a "Specified method is not supported" exception:

Message: 
[Test Failure] Specified method is not supported.

  Stack Trace: 
RuntimeMethodInfo.ThrowNoInvokeException()
RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
PropertyInfo.GetValue(Object obj)
ReflectionHelper.GetMemberValue(Object obj, MemberInfo member)
StructuralEqualityComparer`1.CompareStructurally(Object x, Object y, HashSet`1 visited)
StructuralEqualityComparer`1.Equals(T x, T y)
CollectionEquivalencyChecker.CheckUnorderedEquivalenceLinear[TItem](List`1 actualList, List`1 expectedList, IEqualityComparer`1 comparer)
CollectionEquivalencyChecker.CheckUnorderedEquivalence[TItem](List`1 actualList, List`1 expectedList, IEqualityComparer`1 comparer)
CollectionEquivalencyChecker.AreEquivalent[TItem](IEnumerable`1 actual, IEnumerable`1 expected, CollectionOrdering ordering, IEqualityComparer`1 comparer)
IsEquivalentToAssertion`2.CheckAsync(EvaluationMetadata`1 metadata)
Assertion`1.CreateMetadataAndCheckAsync(TValue value, Exception exception)
Assertion`1.ExecuteCoreAsync()
Assertion`1.AssertAsync()

It looks like TUnit is trying to test for structural equality via reflection and cannot read the private field. But I think TUnit should use the specialized Equals(...) method here if the class implements IEquatable.

I saw this merge request: https://github.com/thomhurst/TUnit/pull/1602/changes but I think that only means that you can manually give it a comparer? Shouldn't this work automatic?

Expected Behavior

Test test returns sucess

Actual Behavior

The test throws an unrelated exception due to reflection

Steps to Reproduce

See description

TUnit Version

1.17.54

.NET Version

.NET 10

Operating System

Windows

IDE / Test Runner

Visual Studio

Error Output / Stack Trace

Additional Context

No response

IDE-Specific Issue?

  • I've confirmed this issue occurs when running via dotnet test or dotnet run, not just in my IDE

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions