Question

I'm not probably first who deals with mocking in windows store application for testing purpose. I would like to test my ViewModels and to use some of mocking frameworks to mock them. Of course, all of available (common) frameworks are not able to use in windows store application project. I have one idea how to solve it but I'm not sure that it is best solution. My solution consists of these projects. Main point is to divide presentation layer to two parts :

Presentation - Windows store application

Start-up project that contains only presentation views (Pages) and presentation parts that do not need to be tested. This project has reference to PresentationLogic.

PresentationLogic - Portable Class Library, Targets : Windows store application, .NET Framework 4.5

This project contains all presentation logic like ViewModels, Converters, Helpers etc. that should be tested

UnitTests - Class library

Classical class library containing unit tests with ability to mocking all interfaces from PresentationLogic. This library has reference to PresentationLogic.

It is quite strange to divide Views and ViewModel to two layers but I did not find another solution for this.

Do you have please any idea how to deal with this problem? What about splitting of the presentation layer to the two layers of another project type? Can it cause some problems in further development?

Was it helpful?

Solution

You're definitely on the right track. A couple of notes:

Using MvvmLight (which is available portable, by the way), you can use their built-in ServiceLocator and DependencyInjection to do things like inject test controllers for platform specific processes. This will allow a ton of your logic to remain portable, by defining interfaces and injecting the implementations (including mocked implementations).

Based on your PCL, you will likely (in my experience) be unable to include Converters (which inherit from IValueConverter) in your PCL. The library is generally different between the platforms (especially Silverlight/WinRT/4.5/Mono), as the most common use for them is for UI, such as binding processing. Same with things like DataTemplateSelectors. These will likely have to be rewritten for most of your platforms (though luckily that's not that hard and is still quite a bit of copy-paste).

As to the rest of it, you have it spot on. You can have your Presentation app be Universal, so it can cover both Windows Store and Windows Phone Store apps. The vast majority of your 'business logic' should be in your PCL. You may run into some issues in this regard because sometimes it's just unavoidable to want to put some UI helpers inside the VM for ease of use. If this is absolutely necessary, you can make your Portable ViewModel abstract, then use the Dependency Injection mentioned above to insert the platform-specific implementations. It's quite easy to do and very useful.

The one thing that you are missing is UI tests. You could include them in your unit test class library, or make another Coded UI Test class library, up to you.

Anyway, hope that helps.

OTHER TIPS

FYI, you can now use JustMock in order to mock directly into Windows 8.1 Unit Test projects.

See my answer

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top