chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
If have the following 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 


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);


Any ideas as to what's going on here?

Ed.
asked by loosecoupling@google (600 points)

7 Answers

0 votes
Hi Ed,

The issue you've encountered was caused by a bug. Because Item has SharePointPermission attribute which cause Isolator to think that you have two calls inside the WhenCalled block.

As you already discovered there is a workaround using NonPublic API.

I'll investigate this issue and update this thread after we have fixed the problem.

Regards,

Dror Helper
answered by dhelper (11.9k points)
0 votes
Hi,

We fixed the bug in an internal version - I'm going to email Ed with the patch, and this will be released with the next Isolator version.

Doron
Typemock Support
answered by doron (17.2k points)
0 votes
I am seeing the same problem with our typemock tests.

The bug doesnt seem to be fixed in version 6.0.3 of typemock. What version should it be fixed in? Can you send me the patch as well?
answered by jpennal (800 points)
0 votes
This issue should be fixed in 6.0.3. Can you send a project that reproduce this issue to support AT typemock DOT com so we can investigate it.
answered by dhelper (11.9k points)
0 votes
We're on 6.0.8 and are still encountering the same issue and receiving the same "Cannot dynamically create an instance of System.Void" message.

We're trying to mock the call to the static property getter for the Cache property of HttpRuntime.

Cache fakeCache = new Cache();
Isolate.WhenCalled(() => HttpRuntime.Cache).WillReturn(fakeCache);



Using the non-public API with a call to "get_Cache" served as a workaround, but we'd prefer to use the strongly-typed calls.
answered by pwong (180 points)
0 votes
Hi,

We are working on a fix, and will post here when we are done.

Regards,
Yonatan
Typemock Support
answered by yonatan (1.6k points)
0 votes
Hi,

The problem is caused by an optimization of the clr, only on 64bit processes, using NUnit (MSTest should work fine).
In this specific case, faking the static constructor of HttpRuntime will do the trick, and the aforementioned NonPublic API should work as well.

Regards,
Yonatan
Typemock Support
answered by yonatan (1.6k points)
...