You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
NSubstitute does not overrides private protected virtual methods #612
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.
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?
@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
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.
usingSystem.Runtime.CompilerServices;usingCastle.Core.Internal;usingSystem;usingNUnit.Framework;[assembly:InternalsVisibleTo(InternalsVisible.ToDynamicProxyGenAssembly2)]namespaceCastle.DynamicProxy.Tests{[TestFixture]publicclassBasicClassProxyTestCase:BasePEVerifyTestCase{privateprotectedvirtualvoidPrivateProtectedThrowException()=>thrownewException("I was called");protectedvirtualvoidProtectedThrowException()=>thrownewException("I was called");internalvirtualvoidInternalThrowException()=>thrownewException("I was called");[Test]publicvoidPrivateProtectedVirtualMethodsAreInterceptedCorrectly(){varproxy=generator.CreateClassProxy<BasicClassProxyTestCase>(newDoNothingAndReturnDefaultInterceptor());Assert.DoesNotThrow(()=>proxy.InternalThrowException());Assert.DoesNotThrow(()=>proxy.ProtectedThrowException());Assert.DoesNotThrow(()=>proxy.PrivateProtectedThrowException());}publicclassDoNothingAndReturnDefaultInterceptor:StandardInterceptor{protectedoverridevoidPerformProceed(IInvocationinvocation){objectreturnValue=invocation.ReturnValue;if(returnValue!=null){varreturnValueType=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.
Activity
dtchepak commentedon Mar 10, 2020
Thanks for raising this.
The MSDN documentation is not clear on this, but I don't think
InternalsVisibleToworks withprivate 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?
tpodolak commentedon Mar 18, 2020
@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
PrivateProtectedVirtualMethodCrispyDrone commentedon Sep 12, 2020
@dtchepak
I just created a test solution with 2 projects, one has a class with a
private protectedmember. It is accessible from the other project once it is marked with theInternalsVisibleToattribute.Also I went ahead and tested the same code in the
Castle.Coreproject; I think it might be an issue there.This test fails only on the method that's
private protected.dtchepak commentedon Jan 11, 2021
@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.