Home > database >  Getting possible null reference argument warning with Xunit.assert.source 2.4.2
Getting possible null reference argument warning with Xunit.assert.source 2.4.2

Time:09-28

When I use xunit.assert.source I get a possible null warning (CS6804) for Assert.NotNull

[Fact]
public void DoTest()
{
   int? value = null;

   Assert.NotNull(value); // <-- This line
   int foo = value.Value;
}

This is my csproj file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="xunit.assert.source" Version="2.4.2" />
    <PackageReference Include="xunit.core" Version="2.4.2" />
  </ItemGroup>

</Project>

If I use xunit.assert instead of xunit.assert.source, I do not have this problem.

Any ideas on how to fix this, so I can use xunit.assert.source?

CodePudding user response:

This fixed it:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <DefineConstants>$(DefineConstants)TRACE; XUNIT_NULLABLE</DefineConstants>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <DefineConstants>$(DefineConstants)TRACE; XUNIT_NULLABLE</DefineConstants>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="xunit.assert.source" Version="2.4.2" />
    <PackageReference Include="xunit.core" Version="2.4.2" />
  </ItemGroup>

</Project>


CodePudding user response:

Change

<Nullable>enable</Nullable>

to

<Nullable>disable</Nullable>
  • Related