This is a migrated thread and some comments may be shown as answers.

Error during mocking internal class in Silverlight

3 Answers 98 Views
JustMock Free Edition
This is a migrated thread and some comments may be shown as answers.
Grzegorz
Top achievements
Rank 1
Grzegorz asked on 17 May 2013, 06:04 PM
Hi,

I'd like to mock internal class in test project. I added InternalsVisibleToAttribute to JustMock and my test assembly, but in Silverlight I'm getting this error: TypeLoadException: Access is denied: 'Xtd.SomeClass`2+SomeSubClass[Xtd.Period,Xtd.Container]'.
w System.Reflection.Emit.TypeBuilder.TermCreateClass(RuntimeModule module, Int32 tk, ObjectHandleOnStack type)
w System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
w System.Reflection.Emit.TypeBuilder.CreateType()
w Telerik.JustMock.DynamicProxy.TypeEmitter.CreateType()
w Telerik.JustMock.DynamicProxy.Proxy.CreateType()
w Telerik.JustMock.DynamicProxy.ProxyFactory.Create()
w Telerik.JustMock.DynamicProxy.Fluent.FluentProxy.NewInstance()
w Telerik.JustMock.DynamicProxy.Proxy.Create(Type target, Action`1 action)
w Telerik.JustMock.MockManager.CreateProxy(Type targetType, Container container)
w Telerik.JustMock.MockManager.CreateInstance(Container container)
w Telerik.JustMock.MockManager.SetupMock(Behavior behavior, Boolean static)
w Telerik.JustMock.MockManager.CreateInstance()
w Telerik.JustMock.Mock.Create(Type target, Behavior behavior, Object[] args)
w Telerik.JustMock.Mock.Create[T](Behavior behavior, Object[] args)
w Xtd.Tests.SomeClassTests.Initialize()

Everything works in WPF, but in silverlight no.

This is code which fails under Silverlight:
Mock.Create<SomeClass<Period, Container>.SomeSubClass>(Telerik.JustMock.Behavior.CallOriginal);

I'm using JusMock 2012.3.1016.3.

3 Answers, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 22 May 2013, 12:11 PM
Hi Grzegorz,

You can try and replace the InternalsVisibleTo attribute (this one:
[assembly: InternalsVisibleTo("Telerik.JustMock, PublicKey=0024000004800000940000000602000000240000525341310004000001000100098b1434e598c6" 
+ "56b22eb59000b0bf73310cb8488a6b63db1d35457f2f939f927414921a769821f371c31a8c1d4b"
+ "73f8e934e2a0769de4d874e0a517d3d7b9c36cd0ffcea2142f60974c6eb00801de4543ef7e93f7"
+ "9687b040d967bb6bd55ca093711b013967a096d524a9cadf94e3b748ebdae7947ea6de6622eabf"
+ "6548448e")])
with this: [assembly: InternalsVisibleTo("Telerik.JustMock")]

Changing the above fixes the issue in the latest JustMock official release (JustMock 2013 Q1 SP2). Also, updating your current JustMock version to it is recommendable.

However, I am happy to say that today we plan to release an official internal build of JustMock 2013 Q2. In it, we have included the Silverlight elevated mocking feature. Please, feel free to try it out at any time.

Let me know if there is anything else I could help you with.

Regards,
Kaloyan
Telerik
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
0
Grzegorz
Top achievements
Rank 1
answered on 30 May 2013, 10:18 PM
Thank you for reply,

I have signed assembly and when I change this attribute I'm getting following error:
"Strong-name signed assemblies must specify a public key in their InternalsVisibleTo declarations".

Is there some other solution in this case? (other than removing strong name from my assemblies)
0
Kaloyan
Telerik team
answered on 31 May 2013, 07:28 AM
Hi Grzegorz,

Unfortunately, you won`t be able to apply the InternalsVisibleTo attribute to Silverlight signed assemblies with the free edition of JustMock. For this, you will need the JustMock profiler and the Silverlight elevated mocking feature. Both these, come with the commercial version of JustMock.

As you have stated, one of the possible solutions is to remove the strong names of your Silverlight assemblies.

However, another option for you is to test your Silverlight code inside a full .NET project. If your Silverlight application or class library is in a full .NET solution, you could try the following:
  1. Start by adding new "JustMock Test Project" to the solution.
  2. Replace the existing references with their Silverlight versions.

    For example:

    "System.DLL" should be replaced with the "System.DLL", in most cases coming from "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v5.0\System.dll".

    You can also replace the "Microsoft.VisualStudio.QualityTools.UnitTestFramework" with any other testing framework, like NUnit or MbUnit.

  3. Further, you should add a reference to the Silverlight project, you will be testing.
  4. Finally, you are ready to start writting unit tests against your Silverlight application or class library.

I hope this helps.

Regards,
Kaloyan
Telerik
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
Tags
JustMock Free Edition
Asked by
Grzegorz
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Grzegorz
Top achievements
Rank 1
Share this question
or