NSubstitute.Analyzers icon indicating copy to clipboard operation
NSubstitute.Analyzers copied to clipboard

Analyzer for misuse of `Arg.Any<T>` in xunits `Asert.Throws<T>`?

Open FroggieFrog opened this issue 10 months ago • 1 comments

Is there already an analyzer that schould trigger when the following incorrect use happens? This caused some isssues in my test library and I only found out after commenting out about 50 tests. So an analyzer would be great for that kind of scenario.

using System;
using NSubstitute;
using Xunit;

namespace Tests;

public interface IMyInterface
{
    int MyMethod(string myParameter);
}

public class TestClass
{
    [Fact]
    public void IStItem_GetNext_ShouldThrow()
    {
        // Arrange
        var substitute = Substitute.For<IMyInterface>();

        // Act/Assert
        Assert.Throws<NotImplementedException>(() => substitute.MyMethod(Arg.Any<string>())); // <-- INcorrect | should trigger
        Assert.Throws<NotImplementedException>(() => substitute.MyMethod(string.Empty)); // <-- correct | should NOT trigger
    }
}

FroggieFrog avatar Aug 24 '23 09:08 FroggieFrog