Skip to content

[Bug]: Static types on classes not picking proper type for assertion #5134

@Josehardrock

Description

@Josehardrock

Description

The following class can't get the proper type for getting an assertion with Assert.That().

Expected Behavior

The proper type is recognized and the assertion can be executed.

Actual Behavior

Image

Steps to Reproduce

  1. Create a class with the following kind of type.
    `
    public class Error(string code, string message) : IEquatable
    {
    public static readonly Error None = new(string.Empty, string.Empty);

    public static readonly Error NullValue = new(
    "Error.NullValue",
    "The specified result value is null."
    );

    public string Code { get; } = code;

    public string Message { get; } = message;

    public static implicit operator string(Error error) => error.Code;

    public static bool operator ==(Error? a, Error? b)
    {
    if (a is null && b is null)
    {
    return true;
    }

     if (a is null || b is null)
     {
         return false;
     }
    
     return a.Equals(b);
    

    }

    public static bool operator !=(Error? a, Error? b) => !(a == b);

    public virtual bool Equals(Error? other)
    {
    if (other is null)
    {
    return false;
    }

     return string.Equals(Code, other.Code, StringComparison.Ordinal) && string.Equals(Message, other.Message, StringComparison.Ordinal);
    

    }
    `

  2. Add a static instantiation of the same class with new(x,y).

  3. Create a test as follows` [Test]
    [Category("UnitTests")]
    public async Task ImplicitOperatorShouldConvertToString()
    {
    // Arrange
    Error error = new Error("ErrorCode", "Error Message");

     // Act
     string code = error.Code;
    
     // Assert
     await Assert.That(error).IsNotNull()
         .And.IsTypeOf<Error>()
         .And.Member(x => x.Code, c => c.IsEqualTo(code))
         .And.Member(x => x.Message, m => m.IsEqualTo("Error Message"));
    

    }
    `

TUnit Version

1.13.*, 1.19.22

.NET Version

NET 9.0

Operating System

Windows

IDE / Test Runner

dotnet CLI (dotnet test / dotnet run)

Error Output / Stack Trace

TUnit.Engine.Exceptions.TestFailedException: [Test Failure] AssertionException: Expected to be of type Error
  but Value is of type String, not Error

  at Assert.That(nullValueError).IsNotNull().And.IsTypeOf<Error>().And.Member(x => x.Code, ...).IsEqualTo("Error.NullValue").And.Member(x => x.Message, ...).IsEqualTo("The specified result value is null.")

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

    StalebugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions