diff --git a/UnitTests/MockFixture.cs b/UnitTests/MockFixture.cs index 0060575f1..06efc0a0e 100644 --- a/UnitTests/MockFixture.cs +++ b/UnitTests/MockFixture.cs @@ -1084,5 +1084,42 @@ public interface INewFoo : IFoo public interface INewBar : IBar { } + + // Note that this test requires that there be no [assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")] + // or similar defined in this test assembly. If some other test requires that internals be made + // visible to DynamicProxy, then this test must be disabled. + [Fact] + public void SetupOnInaccessibleMethodThrowsException() + { + var mock = new Mock(); + var error = Record.Exception(() => + { + mock.Setup(m => m.Internal()); + }); + Assert.NotNull(error); + Assert.IsType(error); + Assert.Contains("accessible", error.Message); + Assert.Contains("proxy generator", error.Message); + } + + [Fact] + public void SetupOnAccessibleMethodDoesNotThrowException() + { + var mock = new Mock(); + var error = Record.Exception(() => + { + mock.Setup(m => m.Public()); + }); + Assert.Null(error); + } + + public class Accessibility + { + public class ClassWithAccessibleAndInaccessibleMethod + { + public virtual void Public() => throw new InvalidOperationException("Public"); + internal virtual void Internal() => throw new InvalidOperationException("Internal"); + } + } } }