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

Possible false reporting of NS1001 #148

Closed
manfred-brands opened this issue Oct 20, 2020 · 2 comments
Closed

Possible false reporting of NS1001 #148

manfred-brands opened this issue Oct 20, 2020 · 2 comments

Comments

@manfred-brands
Copy link

We have a Test class implementing an interface. The methods itself are not virtual.

    public interface ITestInterface
    {
        void InterfaceMethod();
    }

    public class TestClass : ITestInterface
    {
        public void InterfaceMethod()
        {
        }
    }

    public sealed class TestClassFixture
    {
        [Test]
        public void TestNS1001()
        {
            TestClass instanceAsClass = Substitute.ForPartsOf<TestClass>();
            ITestInterface instanceAsInterface = Substitute.ForPartsOf<TestClass>();

            instanceAsClass.Received().InterfaceMethod();  // Triggers NS1001
            instanceAsInterface.Received().InterfaceMethod();
        }
    }

The code works either way and as the error message says: Only interface members and virtual, overriding, and abstract members can be intercepted. Either should be fine. Although the documentation says that members should be virtual.

The ISymbolExtensions.IsInterfaceMember checks on the ContainingType but it check if the method implements an interface.

If you agree, I will raise a PR.

@tpodolak
Copy link
Member

tpodolak commented Oct 20, 2020

The warning is not reported for

instanceAsInterface.Received().InterfaceMethod();

because NSubsitute.Analzyers are not yet smart enough to figure out that the substitute was created for class Substitute.ForPartsOf<TestClass>(); ,please take a look here for more detailed explanation.

The code works either way

This actually is not true, because in both cases you are getting false positives. Your test is green, but in fact it should be red, as method InterfaceMethod was never invoked

@manfred-brands
Copy link
Author

Thanks, indeed whether actually called or not, the Received always passes.

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