Upgrading from 5.3.1 to 5.3.4

For some strange reason when my existing projects referenced Type Mock and Type Mock Isolator it would mark Type Mock as copy local true and Isolator as false. However, after upgrading to 5.3.4 when I add a reference the opposite occurs. This causes a problem when upgrading a project and the project continues to use Type Mock as the local copy (5.3.1) and Isolator and the new version (5.3.4). This leads to some strange behaviour. So double check your references after upgrading.
 
Advertisements

“Bad class token” when running (N) unit tests with TypeMock

Had a strange problem today, some of the automated build tests were failing with "Bad class token". However, when I went onto the build machine and ran them via NUnit GUI they worked fine. The difference is that we’d normally run the tests via Type Mock to ensure all the mocks work correctly. When I ran NUnit GUI via TypeMock the tests would fail…well actually sometimes they’d work, sometimes they wouldn’t. Yes that’s a lovely position to be in <gulp>. The obvious cause to these problems was that I’d just upgraded Visual Studio 2008 to SP1. So I manually rebuilt the whole solution and everything seemed to spring back to life again. Odd problem but hopefully now someone else won’t have to sit around scratching their head like I had to!
 
[Edit]
I blogged too soon. Turns out I still have the problem, but only with automated builds, if I build by hand (on the build server) then everything runs fine…weird.

[Edit]
Turns out I "just" needed to upgrade TypeMock to the latest version.

 

Testing ASP.NET Session state part 2

After publishing the first example I thought I’d better show how to check the actual values in the cache too…
       
static void Main(string[] args)
        {
            Page page = new Page();
            MockManager.Init();
            MockObject sessionMock = MockManager.MockObject(typeof(HttpSessionState));
            Mock pageMock = MockManager.MockAll(page.GetType());
            pageMock.ExpectGet("Session", sessionMock.Object);
            sessionMock.ExpectGetIndex("ValueInIndex").Args("IndexKey");
            string value = (string)page.Session["IndexKey"];
            Console.WriteLine(value);
            Console.ReadKey();
            MockManager.Verify();
            MockManager.ClearAll();
        }

Testing ASP.NET session cache

I was asked how to test the ASP.NET Page.Session object. I’m sure there are many ways of doing it, but here is my quick console sample using TypeMock;

 static void Main(string[] args)
        {
            Page page = new Page();
            MockManager.Init();
            MockObject sessionMock = MockManager.MockObject(typeof(HttpSessionState));
            Mock pageMock = MockManager.MockAll(page.GetType());
            pageMock.ExpectGet("Session", sessionMock.Object);
            sessionMock.ExpectGetIndex("bert");
            string value = (string)page.Session["bert"];
            Console.ReadKey();
            MockManager.Verify();
            MockManager.ClearAll();
        }

TypeMock and COM+

A colleague of mine ran into a problem testing a .net component (the server component). The underlying problem was that the .net component was deriving from Enterprise Services, i.e. it behaves like a COM+ component. I believe the issue here is that the profiler style interception used by TypeMock does not prevent the COM+ interception and therefore the "normal" code for server component runs rather than the mocked code. I’ve not looked into a solution for that yet as the COM+ code is only a thin wrapper to other non-COM+ code so we decided to simply not mock it.

Mocking Framework

The projects I work on make use of Unit Testing via the NUnit Framework and the TestDriven Visual Studio addin. However, we haven’t made use of a Mocking Framework and I finally decided to have a look around and a quick experiment.
The basic problem is that you want to test the working of a method/property on a class but you don’t want to let the class call further dependant objects. Why not? The most important reasons for me is that I don’t want to configure a whole system in order to run a simple test.
A number of Mocking Frameworks use the ‘Mock Object Pattern’ that relies upon the dependant service classes always exposing interfaces. I really don’t like this pattern, ok some would argue that it is good OO to always expose an interface, but I don’t like implementing code features for the sake of supporting a framework that isn’t required for the solution. But the real problem is that I now have to pass the interfaced object into the dependant clients…NO!
So I quickly gravitated to TypeMock which uses the CLR Profiler to intercept the calls to the services and inject code to mock the results. It’s very good, however IMO the documentation is surprisingly poor. I recommend reading through the Test Patterns documents but the code snippets do have a few bugs. So I’ve included a VS2005 solution (ConsoleApplicationTypeMock.zip) with this blog entry that implements the first authentication sample. I’ve used it a few times to test my tests when I get bugs with the mocking framework.
Another quick tip, when you do get errors from the Mocking Framework, pay very close attention to the "type" in the message.