Skip to content

NSubstitute does not overrides private protected virtual methods #612

Closed

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" />

Activity

  1. dtchepak commented on Mar 10, 2020

    @dtchepak
    Member

    Thanks for raising this.

    The MSDN documentation is not clear on this, but I don't think InternalsVisibleTo works with private protected. In that case NSubstitute will not be able to substitute for these members.

    @tpodolak once this is confirmed, i'm not sure if this is worth adding to analyzers as normally the class will be in the assembly being tested (rather than the test assembly), so the members won't be directly callable anyway. WDYT?

  2. added
    bug
    on Mar 10, 2020
  3. tpodolak commented on Mar 18, 2020

    @tpodolak
    Member

    @dtchepak I dont see much value in adding this analyzer, this seems to me as a very edge case. Maybe if more issues like that will be reported we could consider writing the analyzer. One way or another, even with analyzer in place, the case shown in the issue wouldnt be reported - as there is no setup done for PrivateProtectedVirtualMethod

  4. CrispyDrone commented on Sep 12, 2020

    @CrispyDrone

    @dtchepak

    I just created a test solution with 2 projects, one has a class with a private protected member. It is accessible from the other project once it is marked with the InternalsVisibleTo attribute.

    Also I went ahead and tested the same code in the Castle.Core project; I think it might be an issue there.

    using System.Runtime.CompilerServices;
    using Castle.Core.Internal;
    using System;
    using NUnit.Framework;
    [assembly: InternalsVisibleTo(InternalsVisible.ToDynamicProxyGenAssembly2)]
    
    namespace Castle.DynamicProxy.Tests
    {
    	[TestFixture]
    	public class BasicClassProxyTestCase : BasePEVerifyTestCase
    	{
    		private protected virtual void PrivateProtectedThrowException() => throw new Exception("I was called");
    		protected virtual void ProtectedThrowException() => throw new Exception("I was called");
    		internal virtual void InternalThrowException() => throw new Exception("I was called");
    
    		[Test]
    		public void PrivateProtectedVirtualMethodsAreInterceptedCorrectly()
    		{
    			var proxy = generator.CreateClassProxy<BasicClassProxyTestCase>(
    				new DoNothingAndReturnDefaultInterceptor()
    			);
    
    			Assert.DoesNotThrow(() => proxy.InternalThrowException());
    			Assert.DoesNotThrow(() => proxy.ProtectedThrowException());
    			Assert.DoesNotThrow(() => proxy.PrivateProtectedThrowException());
    		}
    
    		public class DoNothingAndReturnDefaultInterceptor : StandardInterceptor
    		{
    			protected override void PerformProceed(IInvocation invocation)
    			{
    				object returnValue = invocation.ReturnValue;
    
    				if (returnValue != null)
    				{
    					var returnValueType = returnValue.GetType();
    
    					if (returnValueType.IsValueType)
    					{
    						invocation.ReturnValue = Activator.CreateInstance(returnValueType);
    					}
    					else
    					{
    						invocation.ReturnValue = null;
    					}
    				}
    			}
    		}
    	}
    }

    This test fails only on the method that's private protected.

  5. dtchepak commented on Jan 11, 2021

    @dtchepak
    Member

    @CrispyDrone Thanks so much for chasing this up with Castle Project and for all your work on it.

    Sorry for the late acknowledgement, I was just going through my inbox and found the notification of your comment. 🤦

    I'll close this for now as it should be sorted by Castle Core 5.x release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions