You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
With nullable reference types enabled, the predicate passed to Is<T>(...) receives T? even when T is a non-nullable reference type. Every predicate that dereferences its argument then needs a null guard to avoid CS8602, which makes the common case noisy:
mock.Save(Is<User>(u =>u.Name=="Alice"));// CS8602: Dereference of a possibly null reference.mock.Save(Is<User>(u =>uis not null&&u.Name=="Alice"));// compiles
During a Moq migration of a large test suite this needed a null guard in a few dozen predicates. Moq's It.Is<User>(u => u.Name == "Alice") passes User, not User?.
Repro (TUnit.Mocks 1.76.45, net10.0, <Nullable>enable</Nullable>, warnings as errors)
When T is a non-nullable reference type, the predicate parameter is T. Is<User?>(u => ...) gives the nullable form when a test wants to handle null. Both forms pass null through at runtime and preserve existing predicate exception propagation; use an explicit null guard to make null a non-match.
Updated the null expectation following the repeated review concern on #7405: generic nullable annotations cannot distinguish the two forms at runtime. Preserving exception propagation matches the existing implementation and avoids hiding predicate bugs. The annotation fix remains the requested ergonomic improvement.
Activity
test(mocks): enforce predicate nullable diagnostics for #7304
docs(mocks): clarify null predicate exception behavior (#7304)
fix(mocks): preserve predicate exception propagation (#7304)
fix(mocks): Is<T>(predicate) passes T, not T?, for non-nullable T (#7405