Export to GitHub

moq - issue #356

Unable to Mock Internal Members in Silverlight


Posted on Dec 19, 2012 by Happy Rhino

What steps will reproduce the problem? 1. Create a C# Silverlight 4 project in Visual Studio 2010 called e.g. MainProject and a Silverlight unit test project called e.g. TestProject. 2. Add an internal interface to MainProject called e.g. ITest: internal interface ITest { int Foo { get; } } And make internal members visible to TestProject and to Moq by editing AssemblyInfo.cs of the MainProject [assembly: InternalsVisibleTo("TestProject")] [assembly: InternalsVisibleTo("DynamicProxyGenAssembly2,PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")] 3. In TestProject add a reference to Moq.Silverlight.dll and the following code in a test class: [TestMethod] public void TestMethod1() { var mock = new Mock<ITest>(); mock.Setup(m => m.foo).Returns(1); Assert.AreEqual(1, mock.Object.foo); }

What is the expected output? The test should pass. What do you see instead? Type MainProject.ITestis not public. Can not create proxy for types that are not accessible. at Castle.DynamicProxy.DefaultProxyBuilder.AssertValidType(Type target) at Castle.DynamicProxy.DefaultProxyBuilder.CreateInterfaceProxyTypeWithoutTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options) at Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyTypeWithoutTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options) at Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyWithoutTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, IInterceptor[] interceptors) at Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyWithoutTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, IInterceptor[] interceptors) at Moq.Proxy.CastleProxyFactory.CreateProxy[T](ICallInterceptor interceptor, Type[] interfaces, Object[] arguments) at Moq.Mock1.&lt;InitializeInstance&gt;b__32() at Moq.PexProtector.Invoke(Action action) at Moq.Mock1.InitializeInstance() at Moq.Mock1.OnGetObject() at Moq.Mock.GetObject() at Moq.Mock.get_Object() at Moq.Mock1.get_Object() at TestProject.Tests.TestMethod1()

What version of the product are you using? On what operating system? Moq 4.0 (latest), Visual Studio 2010, Silverlight 4, Windows 7.

Please provide any additional information below. Please explain why I am unable to mock internal members in Silverlight projects whilst the same is supported in full .NET projects.

Status: New

Labels:
Type-Defect Priority-Medium