Learn by testing
Testing your code always shows up unexpected facets of that code. Attempting to test Universal Apps shows up how far Microsoft still has to go in developing a common platform for Windows and Windows Phone apps.
Universal Apps shared code
Universal Apps was the major developer announcement at Build 2014; “as a developer, this means you can now build XAML and HTML universal apps that run on both Phone and Tablets by sharing a significant amount of code and content”.
Excellent I thought, I can write code once for the functionality shared between my Windows and Windows Phone apps.
Unfortunately the method by which universal apps share code makes this code un-testable. Although universal apps contain a “shared” folder this does not result in any binary output and is incorporated by Visual Studio during the build of the individual Windows and Windows Phone apps by a mechanism that is essential the same as “add as link”. This is one step up from “cut and paste inheritance”.
The reason for this is that, even when the classes have the same name, the RT (Windows Runtime) used by Windows and Windows Phone are different and so the “shared code” has to be compiled against the relevant assemblies at build time.
You could test the code in the shared folder by creating a standard unit test assembly and using “add as link” to include that code but that is too ugly a mechanism to consider seriously.
Back to Portable Class Libraries
To share code that is testable, and there shouldn’t be any un-testable code, the common functionality needs to be in a portable class library and Visual Studio now contains a project template for a “Class Library (Portable for Universal Apps)”. There is, however, no corresponding template for unit test project. The standard unit test project will not reference the Universal Apps portable class library.
To produce a test project you have to use the “Unit Test Library (Windows)” template from the “Store Apps” folder. This template is actually a Windows App project and includes a manifest file, key file and Images folder in addition to unit test class you are actually interested in.
The lack of a clean unit test template shows that Visual Studio’s support for Universal Apps is unfinished. Hopefully this will be rectified in the next update.
What use is the shared code mechanism?
In its present incarnation I am not convinced that the “shared code” for Universal Apps gives developers anything that cannot be achieved using portable class libraries. Indeed the lack of a testing mechanism makes it a step backwards.
Microsoft’s guidance for writing this shared code makes much use of conditional compilation (WINDOWS_APP / WINDOWS_PHONE_APP) to isolate platform specific calls. Again I think this is a step backwards compared to inheriting from classes in the portable library to implement these calls within the respective Windows and Windows Phone apps.
Unfortunately until Windows and Windows Phone apps do share the same RT assemblies than writing “Universal Apps” is going to involve these unsatisfactory code sharing steps.