c# - 对于单元测试,Application.Current 为空

标签 c# mstest nullreferenceexception applicationdomain

我在代码库中有一些方法依赖于 Application.Current.Dispatcher.Invoke... 来确保在 GUI 线程上运行。我目前正在尝试为这些方法编写单元测试,但(正如预期的那样)Application.Current 为 null,因此我得到了 NullReferenceException。

我尝试按照此处的建议在它们自己的 AppDomain 中运行受影响的测试:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/786d5c06-0511-41c0-a6a2-5c4e44f8ffb6/

但是当我这样做时,Application.Current 仍然是 null。启动 AppDomain 不应该为我设置 Application.Current 吗?为什么它仍然是空的?

我的代码: 基类:

[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);
        }
    }
}

调用单元测试(包含在继承自 UnitTest 的类中):

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

最佳答案

所以现在我有一个糟糕的解决方法。我基本上将所有使用 Application.Current 的测试方法的测试移动到单个测试(因此它们都在同一个线程上)并调用 new Application()。

讨厌的代码:

[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 ...
}

关于c# - 对于单元测试,Application.Current 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12625848/

相关文章:

c# - 一个 MSTest 设置文件中的不同 UnitTest 执行

c# - 在构造函数中委托(delegate)调用

c# - 什么是NullReferenceException,如何解决?

c# - 当 WPF DataGrid 选择不可见时,INotifyPropertyChanged 出现空引用异常?

c# - 将动态转换为 Lambda 表达式

c# - 在不公开 Entity Framework 依赖/实现细节的情况下使用 EdmFunctionAttribute?

c# - 为什么 Console.Write 以不同方式对待字符 x266A?

c# - 在 C# 中的 foreach 循环中更新结构

visual-studio - VS2008 UnitTesting - 与 Office 应用程序对象(PowerPoint 等)分离的 RCW

c# - 抛出未处理的异常时未调用 MsTest TestCleanup 方法