I have the following function
public void Reset()
{
DisableModule();
DispatcherHelper.UIDispatcher.Invoke(() =>
{
PixelPointInfoCollection.Clear();
PixelPointInfoCollection.Add(new PointViewModel());
});
_cachedPoints.Clear();
}
The following code gets stuck in the Invoke() method, when running a unit test.
I saw some articles about creating a custom interface on dispatcher and mocking the dispatcher in unit tests.
for example http://blog.zuehlke.com/en/mvvm-and-unit-testing/
Is there no other way? I have a huge code base.. do I really need to change everything now?
Update 18.8.2016
For now here is what I did and it is working
public static class AppServices
{
public static IDispatcher Dispatcher { get; set; }
public static void Init(IDispatcher dispatcher)
{
Dispatcher = dispatcher;
}
}
//this inteface is in order to overrcome MVVM light Dispatcher so we can mock it for unit tests
public interface IDispatcher
{
void Invoke(Action action);
void Invoke(Action action, DispatcherPriority priority);
DispatcherOperation BeginInvoke(Action action);
}
public class DispatcherWrapper :IDispatcher
{
public DispatcherWrapper()
{
DispatcherHelper.Initialize();
}
public void Invoke(Action action)
{
DispatcherHelper.UIDispatcher.Invoke(action);
}
public void Invoke(Action action, DispatcherPriority priority)
{
DispatcherHelper.UIDispatcher.Invoke(action, priority);
}
public DispatcherOperation BeginInvoke(Action action)
{
return DispatcherHelper.UIDispatcher.BeginInvoke(action);
}
}
so inside the app.xaml.cs
I call
AppServices.Init(new DispatcherWrapper());
and in the unit tests I call
AppServices.Init(Substitute.For());
using NSubstitute
Please comment if you thing I'm missing something, I'm worried about how do I make the mocking framework to run the actions I used to do inside the
DispatcherHelper.UIDispatcher.Invoke
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…