Skip to content

NSubstitute does not overrides private protected virtual methods #612

@vadimart92

Description

@vadimart92

Describe the bug
NSubstitute does not overrides private protected virtual methods.

To Reproduce

using System.Runtime.CompilerServices;
using NSubstitute;
using NUnit.Framework;

[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]

namespace TestProject1
{
    public class PrivateProtectedSpec
    {
        private protected virtual void PrivateProtectedVirtualMethod() => throw new Exception("I Was Called");
        protected virtual void ProtectedVirtualMethod() => throw new Exception("I Was Called");

        [Test]
        public void TestPrivateProtectedCall() {
            var system = Substitute.For<PrivateProtectedSpec>();
            Assert.DoesNotThrow(() => system.ProtectedVirtualMethod(), "this one passes");
            Assert.DoesNotThrow(() => system.PrivateProtectedVirtualMethod(), "this fails");
        }

    }
}

Expected behaviour
Calls on private protected virtual methods should not result in calls to the base type.

Environment:

  • NSubstitute version: 4.2.1
  • NSubstitute.Analyzers version: CSharp 1.0.12
  • Platform: netcoreapp3.1 win64

Additional context
<PackageReference Include="NUnit" Version="3.8.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.9.0" />

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugReported problem with NSubstitute behaviour

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions