Reputation: 4611
Assert.NotNull(res);
Assert.Equal(1, res.Foo); // CS8602 warning here
I'm using xunit.assert 2.4.1, .NET 6.0, VS 2022.
When "Ctrl+Click" navigating to the Assert.NotNull() source code I can see it defined as
public static void NotNull(object @object)
while I was actually expecting to see
public static void NotNull([NotNull] object? @object)
From the xunit source code one can see that nullable flavour of methods are used only when XUNIT_NULLABLE
conditional compilation variable is enabled. Can it be that nuget downloaded "non-nullable" version of the xunit.assert package? How can we force a "nullable" version (built with the XUNIT_NULLABLE
defined)?
Upvotes: 5
Views: 2469
Reputation: 1096
Finally, xunit 2.4.2 has been released and Assert.NotNull() plays nicely with C# nullable types!!
The following code is the implementation of NotNull
method in xunit.assert 2.4.2.
/// <summary>
/// Verifies that an object reference is not null.
/// </summary>
/// <param name="object">The object to be validated</param>
/// <exception cref="NotNullException">Thrown when the object reference is null</exception>
#if XUNIT_NULLABLE
public static void NotNull([NotNull] object? @object)
#else
public static void NotNull(object @object)
#endif
{
if (@object == null)
throw new NotNullException();
}
Upvotes: 5