Testing devices using Deployment.Current.Dispatcher.BeginInvoke

I have a Windows Phone 7 Silverlight application that I am trying to run a unit test. My tests fail with the following error:

System.DivideByZeroException: Attempted division by zero.

In the next line:

Deployment.Current.Dispatcher.BeginInvoke(() => RaisePropertyChanged("Lat"));

I assume this is because there is no user interface thread. Do I need to abstract the calls BeginInvokeso that they can mock my test?

Update:

I ended up abstracting, so I could make fun of in unit test. It works great. What do you think?

public class UiDispatcher : IUiDispatcher
{
    public void InvokeOnUiThread(Action action)
    {
        Deployment.Current.Dispatcher.BeginInvoke(action);
    }
}
+3
source share
2 answers

. , Dispatcher.CheckAccess(), Invoke, :

public void InvokeOnUiThread(Action action) 
{
    if(Deployment.Current.Dispatcher.CheckAccess())
    {
        action();
    } else {
        Deployment.Current.Dispatcher.BeginInvoke(action);
    }
} 
+3

, , DispatcherHelper MVVM Light . , DispatcherHelper , , .

MVVM Light Laurent Bugnion http://mvvmlight.codeplex.com/, WP7 MVVM http://chriskoenig.net/series/wp7.

!

+1

Source: https://habr.com/ru/post/1750821/


All Articles