简体   繁体   English

Application.Current对于单元测试是空的

[英]Application.Current is Null for Unit Tests

I have some methods in the code base that rely on Application.Current.Dispatcher.Invoke... to make sure things run on the GUI thread. 我在代码库中有一些依赖于Application.Current.Dispatcher.Invoke ...的方法来确保在GUI线程上运行。 I am currently trying to write unit tests for these methods but (as expected) Application.Current is null so I'm getting a NullReferenceException. 我目前正在尝试为这些方法编写单元测试但是(正如预期的那样)Application.Current为null所以我得到一个NullReferenceException。

I tried to run the affected tests in their own AppDomain as suggested here: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/786d5c06-0511-41c0-a6a2-5c4e44f8ffb6/ 我试图在他们自己的AppDomain中运行受影响的测试,如下所示: http//social.msdn.microsoft.com/Forums/en-US/wpf/thread/786d5c06-0511-41c0-a6a2-5c4e44f8ffb6/

But Application.Current is still null when I do that. 但是当我这样做时,Application.Current仍为null。 Shouldn't starting up an AppDomain set the Application.Current for me? 不应该启动AppDomain为我设置Application.Current? Why is it still null? 为什么它仍然是空的?

My code: The base class: 我的代码:基类:

[TestClass()]
[Serializable]
public class UnitTest
{
    protected void ExecuteInSeparateAppDomain(string methodName)
    {
        AppDomainSetup appDomainSetup = new AppDomainSetup();
        appDomainSetup.ApplicationBase = Environment.CurrentDirectory;
        AppDomain appDomain = AppDomain.CreateDomain(methodName, null, appDomainSetup);

        try
        {
            appDomain.UnhandledException += delegate(object sender, UnhandledExceptionEventArgs e)
            {
                throw e.ExceptionObject as Exception;
            };

            UnitTest unitTest = appDomain.CreateInstanceAndUnwrap(GetType().Assembly.GetName().Name, GetType().FullName) as UnitTest;

            MethodInfo methodInfo = unitTest.GetType().GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);

            if (methodInfo == null)
            {
                throw new InvalidOperationException(string.Format("Method '{0}' not found on type '{1}'.", methodName, unitTest.GetType().FullName));
            }

            try
            {
                methodInfo.Invoke(unitTest, null);
            }
            catch (System.Reflection.TargetInvocationException e)
            {
                throw e.InnerException;
            }
        }
        finally
        {
            AppDomain.Unload(appDomain);
        }
    }
}

The calling unit test (contained in a class that inherits from UnitTest): 调用单元测试(包含在继承自UnitTest的类中):

[TestMethod()]
public void QualifierViewModel_FlagsAndLoadDatasets()
{
    ExecuteInSeparateAppDomain("TestLoadDataSets");
}

So for now I have a nasty workaround. 所以现在我有一个讨厌的解决方法。 I basically moved all the tests that are testing methods that use Application.Current to a single test (so they will all be on the same thread) and call new Application(). 我基本上将所有使用Application.Current的测试方法的测试移动到单个测试中(因此它们都将在同一个线程上)并调用新的Application()。

The nasty code: 令人讨厌的代码:

[TestClass]
    public class IntegrationTests
    {
        private CedarFile testCedarFile;

        /// <summary>
        /// This test tests methods that utilize Application.Current. 
        /// They all have to be in the same test because they must run on the same thread to use the Application instance.
        /// </summary>
        [TestMethod]
        public void IntegrationTests_All()
        {
            new Application();

            QualifierViewModel_FlagsAndLoadDatasets();
            CurrentFilesViewModel_AddCedarFile();
            CurrentFilesViewModel_AddCedarFileReadOnly();
        }
... the private methods to test ...
}

This workaround works for me: 此解决方法适用于我:

[TestClass]
public class MockAppTests
{
    private static Application application = new Application() { ShutdownMode= ShutdownMode.OnExplicitShutdown };
}

[TestClass]
public class IntegrationTests : MockAppTests
{        
    [TestMethod]
    public void MyTest()
    {
        //test
    }
}

尝试删除Serializable属性,而是从MarshalByRefObject派生UnitTest类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM