Question

In order to get PropertyChanged to fire in NUnit tests, I had to set ShouldAlwaysRaiseInpcOnUserInterfaceThread(false). Are there any repercussions to this when I later use the class as a ViewModel? Maybe I should be setting up a user interface thread in NUnit? Help!

public interface ISomething : INotifyPropertyChanged
{
}

public class Something : MvxNotifyPropertyChanged, ISomething
{
   public Something()
   {
      ShouldAlwaysRaiseInpcOnUserInterfaceThread(false);
   }

   private int _num;
   public int Num
   {
      get { return _num; }
      set { if (_num != value) { _num = value; RaisePropertyChanged(() => Num); }
   }
}
Was it helpful?

Solution

By default MvvmCross marshals calls like RaisePropertyChanged onto the UI thread for the convenience of developers.

If you want to disable this on an individual object, you can call ShouldAlwaysRaiseInpcOnUserInterfaceThread(false); for that object (this is a method call rather than a property as properties on ViewModel objects are generally reserved for INotifyPropertyChanged use)

If you want to disable this by default on all objects then you can use Mvx.Resolve<IMvxSettings>().AlwaysRaiseInpcOnUserInterfaceThread = false;

If during testing you want to provide a mock implementation for the UI thread marshalling, then see for example the N=29 video in http://mvvmcross.blogspot.co.uk/ - with some MockDispatcher code inside https://github.com/MvvmCross/NPlus1DaysOfMvvmCross/tree/master/N-29-TipCalcTest/TipCalcTest.Tests

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