Unit testing in WinRT

Topics: Bootstrappers & IoC
Jan 16, 2013 at 8:08 PM
Edited Jan 16, 2013 at 8:09 PM

I am using Caliburn in a WinRT environment and am having a difficult time getting unit tests to work properly against the view models.  The container resolves the objects correctly, but the issues is that the ViewModel does not have any of its initialized or activated methods called, so none of the code under test is called.  There is no publicly accessible activate or initialize method.  Has anyone created unit test against view models that contain code to test outside of the constructor?  Given the async nature of methods, I can't simply call a method and test the result... I have to wait, but I want the method called in the OnActivate or Init.  And I can't call those from the Tests.

Example VM:


public MyViewModel : ViewModelBase
 public MyViewModel(INavigationService service) : base(service)

protected override void OnActivate()
 // call code in an async method to populate MyProperty

private string _myProperty
public string MyProperty
  get{ return _myProperty; }
  set { 
_myProperty = value; NotifyPropertyChanged(); } }

I want to test the value of MyProperty from my unit test but OnActivate isn't being called and I can't call it!

Jan 17, 2013 at 9:15 AM

In your tests you can cast your ViewModel to IActivate and and call Activate() on it.


This will internally call OnInitialize() and OnActivate().
Same is with IDeactivate.