Skip to content

Code fix for NUnit2005 tries to use Is.Empty constraint on incompatible types. #952

@philipp-naused

Description

@philipp-naused

The Is.Empty constraint only works on certain types like string, Guid, IEnumerable, ect.
But the code fix for NUnit2005 will still try to use it if the original code compared against a field called Empty, even if the type is not compatible.

Example:

using NUnit.Framework;
using NUnit.Framework.Legacy;

public enum Range
{
    Empty,
    Full
}

public class Test
{
    [Test]
    public void SampleTest()
    {
        var rangeValue = Range.Empty;
        ClassicAssert.AreEqual(Range.Empty, rangeValue);
    }
}

The code fix (e.g., dotnet format --diagnostics NUnit2005) will replace the assertion with:

Assert.That(rangeValue, Is.Empty);

This test will then fail with:

System.ArgumentException : The actual value must be not-null, a string, Guid, have an int Count property, IEnumerable or DirectoryInfo. The value passed was of type Range.
Parameter name: actual
  at NUnit.Framework.Constraints.EmptyConstraint.ApplyTo[TActual](TActual actual)
  at NUnit.Framework.Assert.That[TActual](TActual actual, IResolveConstraint expression, NUnitString message, String actualExpression, String constraintExpression)
  at Test.SampleTest()

Version: 4.11.2

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions