ViewModelLocator was not found in UWP / WinIot Core

115 views Asked by At

App1 I have an UWP App which uses a ViewModelLocator class (No MVVMLight or Prism). The ViewModelLocator is integrated as resource in the App.xml and used in the DataContext of the View. Running this app in Release and Debug mode works fine.

UnitTestApp1 I have a UnitTest App which references the App1 from above. When running the UnitTests with Release mode, then all tests run. When I run the UnitTests in Debug mode, then I get the error: Cannot deserialize XBF metadata type list as 'ViewModelLocator' was not found in namespace 'App1.UI'. [Line: 0 Position: 0]

This problem is available since Fall Creator as minimal target version. I was reading the in UWP the ResourceDictionaries do not have any code behind and are not initialzed directly. Can this be related to that?

1

There are 1 answers

0
Andrew McFall III On

@Schaf,

The ViewModel needs to be able to access the actual model. You have all of them them being initialized before any data is available for them. That's not how they are intended to be used.

The Model-View-ViewModel construct is meant to allow an aggregation of different data points to present a specific set of information, AND be testable at the same time. In Debug mode, the Resources are not used, because that is essentially a set of static objects (images, lists that don't change, etc.) that are called on at actual runtime.

Additionally, in your scenario, it sounds like your data access is integrated into the ViewModel itself. Testing in Debug mode is supposed to be White-Box, to ensure that the flow, and transformation, of data is easily accessible from beginning to end. By default, this requires that the classes under test (the ViewModels in this case) must be accessible directly from the Test Harness, and thus must be able to be instantiated apart from the overall application context (where the application resources live), which isn't fully assembled in Debug mode.

So to answer your question, yes the inability to test your ViewModels in Debug mode is directly related to them being underneath the ResourceDictionary. If you pull your ViewModels out into their own folder in the solution, at the same level as your model, you should be able to reach them in Debug mode, and test not only the data access but that the information that is populating each ViewModel is the correct set of information, to satisfy the Business rules that you are trying to meet.