Tag Archives: typemock

Typemock: System.NotSupportedException: Cannot dynamically create an instance of System.Void.

Had a problem mocking a call to SPWorkflowActivationProperties.Item today. Turns out to be a bug in the TypeMock library.

Problematic unit test

[Test, Isolated]
        public void PublishToExternalDocumentLibrary_Calls_CopyTo_Correctly()
        {
            ApprovalWorkflow approvalWorkflow = Isolate.Fake.Instance<ApprovalWorkflow>(Members.CallOriginal, ConstructorWillBe.Called);
            SPWorkflowActivationProperties activationProperties = Isolate.Fake.Instance<SPWorkflowActivationProperties>(Members.CallOriginal, ConstructorWillBe.Called);
            SPListItem item = Isolate.Fake.Instance<SPListItem>();

            Isolate.WhenCalled(() => approvalWorkflow.PublishWhenComplete).WillReturn(true);
            Isolate.WhenCalled(() => approvalWorkflow.PublishDocumentLibraryUrl).WillReturn(ExternalDocumentLibraryUrl);
            Isolate.WhenCalled(() => approvalWorkflow.WorkflowActivationProperties).WillReturn(activationProperties);
            Isolate.WhenCalled(() => activationProperties.Item).WillReturn(item);
           
            Isolate.WhenCalled(() => item.Name).WillReturn("test.docx");
           
            // Fire the workflow completed event
            Isolate.Invoke.Event(() => approvalWorkflow.Completed += null, new object[] { this, EventArgs.Empty });

            Isolate.Verify.WasCalledWithExactArguments(() => item.CopyTo("http://xdev03.trading.ad.int.corp.local/Policies/test.docx"));
        } 

When executed it throws the following exception:

System.NotSupportedException: Cannot dynamically create an instance of System.Void.
at System.RuntimeType.CreateInstanceCheckThis()
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache)
at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache)
at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at hi.b(MethodBase A_0)
at hi.a(MethodBase A_0, fk A_1)
at a.a(fk A_0)
at a.c(fk A_0)
at fz.a(fk A_0)
at fz.a(Object A_0, MethodBase A_1, Object[] A_2)
at fz.a(Object A_0, String A_1, String A_2, MethodBase A_3, Object[] A_4, Object A_5)
at Microsoft.SharePoint.Security.SharePointPermissionAttribute.CreatePermission()
at System.Security.PermissionSet.CreateSerialized(Object[] attrs, Boolean serialize, Byte[]& nonCasBlob, PermissionSet& casPset, HostProtectionResource fullTrustOnlyResources)
at Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties.get_Item()
at Total.SharePoint.Approval.Workflow.Test.ApprovalWorkflowFixture.<>c__DisplayClassd.<PublishToExternalDocumentLibrary_Calls_CopyTo_Correctly>b__9() in ApprovalWorkflowFixture.cs: line 44
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)
at Total.SharePoint.Approval.Workflow.Test.ApprovalWorkflowFixture.PublishToExternalDocumentLibrary_Calls_CopyTo_Correctly() in ApprovalWorkflowFixture.cs: line 0 

Fix:
I can work around this issue by replacing the line:

Isolate.WhenCalled(() => activationProperties.Item).WillReturn(item);

With:

Isolate.NonPublic.WhenCalled(activationProperties, "get_Item").WillReturn(item); 

See thread on Typemock forum

Advertisements
Tagged ,