Thursday, April 03, 2008

TypeMock - It's well ace.

Recently I purchased a fantastic mock framework called TypeMock and I will recommend any developer who wishes to improve their testing procedures to consider it. There are a number of bloggers out there with plenty of code examples, but here is my contribution. Today I needed to figure out a way that I can mock a SharePoint SPSite obejct, specifically a site object returned via SPContext.Current.Site. I wanted to do this in order for me to be able to run unit tests against my web parts. Before using TypeMock, the only way I could know what my code was up to was by using trace files. Now, I'm able to physically run the web part's code in real time and see whats going on! :) Ok, so here's the test:
    Public Sub SPContext_MockInstance_Test()
        Dim mockSPSite As Mock = MockManager.MockObject(Of SPSite)(New String() {"http://mycomputer:24000"})
        Using recorder As New RecordExpectations
            Dim dumm As SPSite = SPContext.Current.Site
            recorder.Return(mockSPSite.MockedInstance)
        End Using
        Dim target As BitsAndBobs = New BitsAndBobs
        target.Testy()
        If mockSPSite.MockedInstance IsNot Nothing Then
            DirectCast(mockSPSite.MockedInstance, SPSite).Dispose()
        End If
    End Sub
OK, so the test code works by mocking the call to SPContext.Current.Site, and when that happens, rather then returning that site object (which never exists anyway) the TypeMock framework returns my testing instance of an SPSite object. This is a very cool feature for me, since I've found testing web parts to be a bit of pain. Looking up articles that show you how to attach the debugger to the w3wp.exe process is handy, but it's so fiddly and often doesn't work (well, for me anyway!), so this is a big improvement on that, and hopefully will speed up my testing and allow me to use various scenarios to test the code.

Friday, March 21, 2008

Why Not VB.NET Books?

What’s with this trend of book publishers bringing out new books on topics (such as ASP.NET, LINQ, etc) that are C# focused, and only after about 2–3 months later does the VB.NET version of the same book make an appearance?

This sucks and is totally unfair.

Recently I looking forward to some LINQ related books from APress, only to find out that they’re for C# devs. Normally this isn’t a problem, as it’s quite easy to transfer the C# code to VB.NET. But I was thinking, why the hell should I be doing this anyway? Both the C# and VB.NET books should be made available at the same time.

Tuesday, March 18, 2008

It's Been A While

Having not touched this blog for around 3.5 years, I thought it might be time a made another appearance!

So, with my first blog entry in a while, I thought I'd start off with a mention of a really useful utility that I came across last week called LINQPad.

image

If you're into writing LINQ queries then this tool will really help your LINQ skills. LINQPad isn't simply a Notepad for LINQ though, it has some really cool features.

No Need To Install

Once you download LINQPad there's no installer to run, just run the EXE itself and off you go.

Database Connectivity and LINQ To SQL Features

In this screenshot I've configured LINQPad to connect to the AdventureWorks database on my SQL Server instance:

image

From here I can drag tables into the query editor and use those in my queries. LINQPad automatically takes care of the underlying database connecting for you. Check out this example which returns the Title column value for all records in the Employees table:

image

Note the use of the Dump() method call, this displays the content of j into the LINQPad console window. This query results in the following output:

image

A cool feature is the SQL button found here:

image

Pressing this button will give you the SQL syntax for the LINQ to SQL query in the editor:

image

This is just a taster of how handy this utility could be for you. Check it out.

Friday, December 03, 2004

Great VS IDE Addin

Found on the GotDotNet site a great addin – Boot Addins. I would give the URL but it won’t work anymore. The GotDotNet search workspaces feature never seems to work. If you can find the workspace page for the Boot addins project, you will find items such the ability to play movie/music file directly in the VS IDE! very cool.