Hi
I tried 2 methods of mocking out static calls on SPSecurity.RunWithElevatedPrivileges on Sharepoint 2010:
1. Isolate.WhenCalled(() => SPSecurity.RunWithElevatedPrivileges(() => { })).DoInstead((ctx) => Console.WriteLine("called"));
2. var mock = MockManager.Mock(typeof(SPSecurity), Constructor.Mocked);
mock.MockMethodCalled += new MockMethodCalledEventHandler(mock_MockMethodCalled);
both of them fail with error:
Test 'ConsoleApplication1.Tests.test1' failed: System.TypeInitializationException : The type initializer for 'Microsoft.SharePoint.SPSecurity' threw an exception.
----> TypeMock.TypeMockException :
*** Faking behavior on struct Microsoft.SharePoint.SPSecurity+SPOperationEntry is not supported in the following cases:
1. Faking behavior on more than one struct of the same type.
2. Faking behavior on more than one method on the same struct.
at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode)
Program.cs(27,0): at ConsoleApplication1.Tests.<test1>b__1()
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Boolean A_5, Object[] A_6)
at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected, Boolean isInterceptedType)
Program.cs(24,0): at ConsoleApplication1.Tests.test1()
--TypeMockException
at f9.a(Type A_0, Object A_1, MethodBase A_2, String A_3, TypeParams A_4)
at f9.a(Object A_0, String A_1, String A_2, MethodBase A_3, Object[] A_4, Object A_5)
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Boolean A_5, Object[] A_6)
at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected, Boolean isInterceptedType, Object p1, Object p2, Object p3, Object p4, Object p5)
at Microsoft.SharePoint.SPSecurity.SPOperationEntry..ctor(SPOperationCode code, SPOperationType type, Boolean bSiteAdmin, Boolean bSlowOprations, SPBasePermissions perm)
at Microsoft.SharePoint.SPSecurity..cctor()
My Question: Is it possible to fake this call? And how?