Tag Archives: unit testing

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 ,

Unit testing for SharePoint

Typemock are offering their new product for unit testing SharePoint called Isolator For SharePoint, for a special introduction price. it is the only tool that allows you to unit test SharePoint without a SharePoint server. To learn more click here.

The first 50 bloggers who blog this text in their blog and tell us about it, will get a Full Isolator license, Free. for rules and info click here.

Continue reading

Tagged ,

WatiN supports Firefox!

Back in March I finished developing a release of the WatiN framework that enabled testing in Firefox in addition Internet Explorer (already supported). This was the first time I’ve been involved in collaborative open source development. I worked with Jeroen van Menen, the creator of the WatiN project using Basecamp for project management and sourceforge svn for source control. It was also my first time using Basecamp which worked out pretty well for us. It’s simplicity is what I like. You basically just manage a bunch of grouped lists:

WindowClipping (11)

WindowClipping (12)

Anyhow… It’s now pretty straight forward to make a unit test execute under both IE and firefox

[Test] public void SearchForWatiNOnGoogleVerbose() { using (IBrowser ie = BrowserFactory.Create(BrowserType.InternetExplorer)) { ie.GoTo("http://www.google.com"); ie.TextField(Find.ByName("q")).Value = "WatiN"; ie.Button(Find.ByName("btnG")).Click(); Assert.IsTrue(ie.ContainsText("WatiN")); } using (IBrowser firefox = BrowserFactory.Create(BrowserType.FireFox)) { firefox.GoTo("http://www.google.com"); firefox.TextField(Find.ByName("q")).Value = "WatiN"; firefox.Button(Find.ByName("btnG")).Click(); Assert.IsTrue(firefox.ContainsText("WatiN")); } }

It’s also quite trivial to collapse these two using constructs into a single chunk of code for better maintainability etc..

Check out the WatiN Firefox Community Technology Preview for more information.

Tagged , ,