Thursday, April 12, 2012

Unit testing smart device projects - TestContext error

A couple of days ago I started creating unit tests for a Windows Mobile smart device project that I have been working on in Visual Studio 2008. I generated a unit test method for one of the methods in one of the classes and attempted to run the test. This is when I ran into an error that took me several hours to solve, so hopefully posting this helps someone.

The test method failed to run due to the following error:

Error: System.ArgumentException: Object of type 'Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestAdapterContext' cannot be converted to type 'Microsoft.VisualStudio.TestTools.UnitTesting.TestContext'


Googling the error returned results that suggested it was caused by upgrading a test project from Visual Studio 2005 format to 2008, and that the reference to the UnitTestFramework.dll was still for the old version (8.0) and required updating. This did not apply to me as I had not upgraded my test project from VS2005. So I continued searching.

After hours of searching for other causes for the error without success, I stumbled upon the following references in the smart device project:

Microsoft.WindowsMobile.dll
Microsoft.WindowsMobile.Status.dll

Visual Studio was obtaining these references from the following folder:

C:\Program Files\Windows Mobile 5.0 SDK R2\Managed Libraries

However, the smart device project's target platform was the Windows Mobile 6 Professional SDK. I didn't notice this before as I did not originally create the smart device project. So I removed the references to the two DLLs and readded them from the following folder:

C:\Program Files\Windows Mobile 6 SDK\Managed Libraries

After rebuilding the smart device project, running the test method succeeded.