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

NS2002 falsely triggered when substituting an interface #205

Open
davidkeaveny opened this issue Jun 6, 2023 · 5 comments
Open

NS2002 falsely triggered when substituting an interface #205

davidkeaveny opened this issue Jun 6, 2023 · 5 comments

Comments

@davidkeaveny
Copy link

davidkeaveny commented Jun 6, 2023

I have a simple interface and a class that dispatches against it:

public interface IReportRunner
{
  void Execute(int year, int month);
}

public class ClassThatTakesInterfaceDependency
{
  private readonly IReportRunner _runner;

  public ClassThatTakesInterfaceDependency(IReportRunner runner)
  {
    _runner = runner;
  }

  public void DoStuffThatInvokesInterface(DateTime now)
  {
    _runner.Execute(now.Year, now.Month);
  }
}

When I create a test that exercises the interface, I'm getting an NS2002 error when creating the substitute:

The number of arguments passed to NSubstitute.Substitute.For do not match the number of constructor arguments for IReportRunner. Check the constructors for IReportRunner and make sure you have passed the required number of arguments.

public class WhenRunningMyTest
{
  [Fact]
  public void ItShouldExecuteSuccessfully()
  {
    // Arrange
    var substitute = Substitute.For<IReportRunner>();
    var sut = new ClassThatTakesInterfaceDependency(substitute);
    
    // Act
    sut.DoStuffThatInvokesInterface(new DateTime(2023, 6, 5));

    // Assert
    substitute.Received(1).Execute(2023, 6);
  }
}

Given that it is substituting an interface, it shouldn't be complaining about constructor parameters.

I am using NSubstitute 5.0.0 with NSubstitute.Analyzers 1.0.16.

@tpodolak
Copy link
Member

tpodolak commented Jun 8, 2023

hi @davidkeaveny
I cannot repro this - no warnings reported both when testing with IDE and with integration tests
image

@DrunkRussianGun
Copy link

Hello
I sometimes run into the same problem. That's a heisenbug so it can be difficult to reproduce even with the same code example, but it appears regularly.

@SonicGD
Copy link

SonicGD commented Jun 28, 2023

Hello. Can confirm. Sometimes these errors just appears. Only Rider cache invalidation helps.

@tpodolak
Copy link
Member

There is not much I can do here. This seems to be Rider Roslyn analyzers support issue rather than NSubstitute.Analyzers bug. I will send bug report to Jetbrains if I encounter this problem on my own - as reporting this without any logs doesnt make sense. Alternatively @SonicGD or @davidkeaveny you can report this on your own https://youtrack.jetbrains.com/issues/RIDER and link the issue here

@hhyyrylainen
Copy link

I just updated from .NET 7 to .NET 8 and I saw this start happening. Restarting Rider with cache invalidation cleared this up.

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

5 participants