6

I'm trying to create a mock HttpContextBase for unit test.

var fakePrinciple = new GenericPrincipal(
           new GenericIdentity(userId), 
           rolesList.ToArray());            
var mockHttpContext = new Mock<HttpContextBase>();
mockHttpContext.Setup(t => t.User).Returns(fakePrinciple);
HttpContextBase mockedContext = mockHttpContext.Object;

The unit test fails at the last statement with

threw exception: System.ArgumentException: Unable to obtain public key for StrongNameKeyPair..

System.Reflection.StrongNameKeyPair.nGetPublicKey(Boolean exported, Byte[] array, String container) System.Reflection.StrongNameKeyPair.get_PublicKey() System.AppDomain.InternalDefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access, String dir, Evidence evidence, PermissionSet requiredPermissions, PermissionSet optionalPermissions, PermissionSet refusedPermissions, StackCrawlMark& stackMark, IEnumerable`1 unsafeAssemblyAttributes) System.AppDomain.DefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access) Castle.DynamicProxy.ModuleScope.CreateModule(Boolean signStrongName) Castle.DynamicProxy.ModuleScope.ObtainDynamicModuleWithStrongName() Castle.DynamicProxy.ModuleScope.ObtainDynamicModule(Boolean isStrongNamed) Castle.DynamicProxy.Generators.Emitters.ClassEmitter.CreateTypeBuilder(ModuleScope modulescope, String name, Type (blah blah snip)

I googled and the suggestions here don't seem to work (change RSA folder security setting etc) http://groups.google.com.br/group/castle-project-users/browse_thread/thread/85685cf32a795158

Am I correct to think that because HttpContextBase is part of System.Web.Abstraction, which is a signed assembly. Moq will actually attempt to sign the dynamic assembly, and fail?

Rokey Ge
  • 681
  • 8
  • 18
  • FWIW, I have used Moq to mock HttpContextBase lots of times and never had that issue. Whatever your issue is, it's not general for the combination of Moq and HttpContextBase. – Mark Seemann Mar 11 '10 at 14:32
  • The trick is that the permissions need to be set on MachineKeys folder not RSA. The post from Ayende does not make this completely obvious if you're not carefull reader. – Piotr Owsiak Jul 21 '11 at 10:52
  • So glad I found this, ugh, two days and finally fixed :D. Just wanted to add some more references that helped me, in case anyone else runs into this: http://ansaurus.com/question/3154345-strong-name-keys-on-windows-7 | http://msdn.microsoft.com/en-us/library/bb909654(v=vs.90).aspx really helped me zero in on the fix that worked for me. – Brett Rossier Apr 09 '14 at 21:28

1 Answers1

10

MoQ uses Castle DynamicProxy for generating mocks at runtime. Rhino Mocks uses the same library for the same purpose. If you check here:

http://ayende.com/Blog/archive/2006/06/09/UnableToObtainPublicKeyForStrongNameKeyPair.aspx

you'll see that the issue is one of permissions to the machine key store. Whatever user account is running the test must have permission to create and delete keys in the store.

You can find much more details about this issue here: http://groups.google.co.uk/group/RhinoMocks/browse_thread/thread/26df68ff01567509/5ddebf407228edc4