Using the information of Panagiotis Kanavos and Stephen Cleary, I can write my test methods as follows:
[TestMethod] public void MyTest() { Helper.RunInWpfSyncContext( async () => { Assert.IsNotNull( SynchronizationContext.Current ); await MyTestAsync(); DoSomethingOnTheSameThread(); }); }
The internal code now works in the context of WPF synchronization and handles all exceptions used for MSTest. Helper Method from Stephen Toub:
using System.Windows.Threading; // WPF Dispatcher from assembly 'WindowsBase' public static void RunInWpfSyncContext( Func<Task> function ) { if (function == null) throw new ArgumentNullException("function"); var prevCtx = SynchronizationContext.Current; try { var syncCtx = new DispatcherSynchronizationContext(); SynchronizationContext.SetSynchronizationContext(syncCtx); var task = function(); if (task == null) throw new InvalidOperationException(); var frame = new DispatcherFrame(); var t2 = task.ContinueWith(x=>{frame.Continue = false;}, TaskScheduler.Default); Dispatcher.PushFrame(frame); // execute all tasks until frame.Continue == false task.GetAwaiter().GetResult(); // rethrow exception when task has failed } finally { SynchronizationContext.SetSynchronizationContext(prevCtx); } }
source share