Skip to content

[Bug]: Member(...) on nullable string property produces nullable warnings #5190

@mzwierzchlewski

Description

@mzwierzchlewski

Description

Using TUnit 1.19.74, Member(...) on a property declared as string? produces nullable warnings. The warnings appear when asserting on the nullable member with both inferred and explicit generic arguments (although you get different warnings in both cases).

Expected Behavior

Neither of these calls produces a warning.

Actual Behavior

The warnings occur on these specific lines:

await Assert.That(model)
    .Member(x => x.Schema, schema => schema.IsEqualTo("dbo"));

Produces:
CS8603: Possible null reference return. - this points to the selector lambda x => x.Schema.

await Assert.That(model)
    .Member<Model, string?>(x => x.Schema, schema => schema.IsEqualTo("dbo"));

Produces:
CS8620: Argument of type 'IAssertionSource<string?>' cannot be used for parameter 'source' of type 'IAssertionSource<string>' ...
CS8619: Nullability of reference types in value of type 'StringEqualsAssertion' doesn't match target type 'Assertion<string?>'.

Steps to Reproduce

Use the following minimal test:

#:package TUnit@1.19.74

#nullable enable
using TUnit.Assertions;

public class Tests
{
    [Test]
    public async Task Test()
    {
        var model = new Model
        {
            Schema = "dbo"
        };

        await Assert.That(model)
            .Member(x => x.Schema, schema => schema.IsEqualTo("dbo"));

        await Assert.That(model)
            .Member<Model, string?>(x => x.Schema, schema => schema.IsEqualTo("dbo"));
    }
}

sealed class Model
{
    public string? Schema { get; set; }
}

TUnit Version

1.19.74

.NET Version

.NET10

Operating System

macOS

IDE / Test Runner

JetBrains Rider

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

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions