Skip to content

TUnit.Mocks: generated generic members drop nullable 'class?' constraint (CS8634) #7305

Closed

Description

@thomhurst

Description

For a generic method constrained where T : class?, the generated setup/verify extension is constrained where T : class. Setting up the method with a nullable type argument then raises CS8634, which fails builds that treat warnings as errors.

Repro (TUnit.Mocks 1.76.45, net10.0, <Nullable>enable</Nullable>)

public record Foo(string N);
public interface ISvc { Task<T> Get<T>(string key) where T : class?; }

var mock = Mock.Of<ISvc>();
mock.Get<Foo?>(Any()).Returns((Foo?)null);
warning CS8634: The type 'Foo?' cannot be used as type parameter 'T' in the generic type or method
'ISvc_MockMemberExtensions.Get<T>(Mock<ISvc>, Arg<string>)'. Nullability of type argument 'Foo?' doesn't match 'class' constraint.

Expected

The generator copies the constraint's nullability (class?, and likewise notnull / nullable struct forms) onto the generated members.

Activity

  1. added
    bugSomething isn't working
    P2Medium: edge case, mode divergence, flaky test, or workaround exists
    on Oct 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Medium: edge case, mode divergence, flaky test, or workaround existsbugSomething isn't workingin-progressWork is in progress

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions