Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
FroggieFrog opened this issue Aug 24, 2023 · 1 comment
Open

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

FroggieFrog opened this issue Aug 24, 2023 · 1 comment

Comments

@FroggieFrog
Copy link

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 FroggieFrog changed the title Analayzer for misuse of Arg.Any<T> in xunits Asert.Throws<T>? Analyzer for misuse of Arg.Any<T> in xunits Asert.Throws<T>? Aug 24, 2023
@tpodolak
Copy link
Member

tpodolak commented Sep 6, 2023

Is there already an analyzer that schould trigger when the following incorrect use happens?

No, we dont have analyzer for these cases. We actually wanted to have one and attempted to write it but after testing on actual codebases we ended up with much simplified version which only check usages on non-virtual members. Here are relevant issue and pr with all necessary discussions why we ended up with rather basic analyzer for arg misuse
#35
#120

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants