Skip to content

[Bug]: CS8615 for generated nullable event mocks #5424

@Kaskadee

Description

@Kaskadee

Description

The nullability of event members in an interface is ignored in the generated mock implementation causing the warning CS8615: Nullability of reference types in type doesn't match implemented member 'event EventHandler<string>? IFoo.Something'

public sealed class IFooMock : global::TUnit.Mocks.Mock<global::Sandbox.IFoo>, global::Sandbox.IFoo
{
    [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
    internal IFooMock(global::Sandbox.IFoo mockObject, global::TUnit.Mocks.MockEngine<global::Sandbox.IFoo> engine)
        : base(mockObject, engine) { }

    event global::System.EventHandler<string> global::Sandbox.IFoo.Something { add => Object.Something += value; remove => Object.Something -= value; } // Should be `EventHandler<string>?` instead.
}

Expected Behavior

The generated event should use the same nullability as declared in the interface

Actual Behavior

The generated event is non-nullable even though it is declared as nullable in the interface

Steps to Reproduce

using TUnit.Mocks.Generated.Sandbox;

namespace Sandbox;

public interface IFoo {
    event EventHandler<string>? Something;
}

public class TestClass {
    [Test]
    public async Task SomeTest() {
        IFooMock foo = IFoo.Mock();
    }
}

TUnit Version

1.28.7-beta

.NET Version

.NET 10.0

Operating System

Windows

IDE / Test Runner

JetBrains Rider

Error Output / Stack Trace

Restore complete (1.3s)
  Sandbox net10.0 succeeded with 1 warning(s) (1.8s) → Sandbox/bin/Debug/net10.0/Sandbox.dll
    /mnt/x/Sandbox/Sandbox/obj/Debug/net10.0/TUnit.Mocks.SourceGenerator/TUnit.Mocks.SourceGenerator.MockGenerator/Sandbox_IFoo_Mock.g.cs(12,72): warning CS8615: Nullability of reference types in type doesn't match implemented member 'event EventHandler<string>? IFoo.Something'.

Build succeeded with 1 warning(s) in 3.6s

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