Pex & Moles with SharePoint – A Quick Example

I recently demoed moles against the SharePoint 2007 object model. I demoed testing a contrived method that simple returned true if the number of items in a SharePoint list was greater than 20. Code is below…


private const int ItemsPerPage = 10;

public bool ShowPagination()
    using (SPSite siteCollection = new SPSite(“
        using (SPWeb web = siteCollection.OpenWeb())
            SPList list = web.Lists[“MyList”];

            if (list.ItemCount > ItemsPerPage)
                return true;

    return false;

This is a difficult method to test, as you cannot mock out the SharePoint OM code (as it’s not written to an interface etc). As you are tied to SharePoint, you would need to alter the SharePoint list to test boundary conditions. This is where moles come in…

I generated a mole dll against the Microsoft.SharePoint dll in my test project, and was then able to intercept SharePoint OM calls via lambdas. The example below illustrates a test that force the value of the list.ItemCount to return a known value. Note that in my test, the SharePoint OM was not called, instead only the Mole classes were called (via a detour).

public void ShowPaginationReturnsFalseWhenLessThanTenItems()
    // Arrange

    // Use initilizers and lamdba functions to set up moles
    // Only “mole” functionality that is used by code
    MSPSite.ConstructorString = (site, url) => new MSPSite(site)
        OpenWeb = () => new MSPWeb
            ListsGet = () => new MSPListCollection
                ItemGetString = id => new MSPList
                    ItemCountGet = () => 9
            Dispose = () => { }

        Dispose = () => { }

    // Act
    TableLogic tableLogic = new TableLogic();
    bool showPagination = tableLogic.ShowPagination();

    // Assert
    Assert.IsTrue(showPagination == false, “The page will be paginated”);

SharePoint 2010: Provisioning a new site using a custom template

In SharePoint 2010, stps are no longer used. Instead site templates are saved as wsp files.

To create a new site collection using one of these template through the UI, you need to create a site collection through central admin that has no template selected. Once completed, browse to the site collection. From here, you are prompted to select a template. Instead of doing this, click the link to “solution gallery” and Upload your wsp file (previously exported). Once complete, go back to the choose template screen (at the root of the site collection) and select you new template from the “custom” tab.


Below is some powershell script that facilitates this…

[void][System.Reflection.Assembly]::Load(“Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)
New-SPSite -Url “
http://localhost:81/sites/robtest2″ -OwnerAlias “Administrator” -Description “Rob Test Site” -Name “Rob Test” -Language 1033
Add-SPUserSolution -LiteralPath “C:\Users\Administrator\Desktop\RobTestX.wsp” -Site “
Install-SPUserSolution -Identity “RobTestX.wsp” -Site “
$site = New-Object Microsoft.SharePoint.SPSite(“
$web = $site.OpenWeb();
$template = $web.GetAvailableWebTemplates(1033) | Where-Object {$_.Name -like “{*” }
$templateName = $template.Name

Quicknote: Extending a VHD

Just a note as I’ve had to extend a fixed size VHD. I used to expand the actual VHD file. This extending the partition, but did not extend the volume. To complete the process, I needed to boot the VHD and extend the volume (C:).

As I wanted to extend my boot volume (C:), I could not extend onto this using Disk Management (from Computer Management). Instead I had to use the Diskpart tool (access via the command line when ran as Administrator).

I then ran the following commands:

diskpart (this takes you to the diskpart prompt).

list disk
select disk 2
detail disk
select volume 3
detail volume
detail volume

Once complete, my fixed size VHD was expanded to  60Gb

Related link: