0

I try to unit-test a Gui application code that uses Application.Current.Dispatcher.Invoke() and would like to use the solution provided by @informatorius in the similar thread Using the WPF Dispatcher in unit tests. The code is listed below.

The problem I have is that Application is not resolved, even if I add using System.Windows. Is there some special mechanism to resolve Application from within a class library that defines the testcases ? I have the MSTest.TestFramework and MSTest.TestAdapter packages installed.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass] 
public class ApplicationInitializer
{
    [AssemblyInitialize]
    public static void AssemblyInitialize(TestContext context)
    {
        var waitForApplicationRun = new TaskCompletionSource<bool>();
        Task.Run(() =>
        {
            var application = new Application();
            application.Startup += (s, e) => { waitForApplicationRun.SetResult(true); };
            application.Run();
        });
        waitForApplicationRun.Task.Wait();
    }
    [AssemblyCleanup]
    public static void AssemblyCleanup()
    {
        Application.Current.Dispatcher.Invoke(Application.Current.Shutdown);
    }
}

[TestClass]
public class MyTestClass
{
    [TestMethod]
    public void MyTestMethod()
    {
        // implementation can access Application.Current.Dispatcher
    }
}
Konrad Eisele
  • 3,088
  • 20
  • 35

1 Answers1

0

Answer pointed me into the right direction: using System.Windows is not enough, I also needed to add reference to PresentationFramework to the project. Dont really understand the auto magic behind that.

Konrad Eisele
  • 3,088
  • 20
  • 35