Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ManagementEventWatcher导致单元测试退出System.AppDomainUnloadexception_C#_Unit Testing - Fatal编程技术网

C# ManagementEventWatcher导致单元测试退出System.AppDomainUnloadexception

C# ManagementEventWatcher导致单元测试退出System.AppDomainUnloadexception,c#,unit-testing,C#,Unit Testing,我正在尝试单元测试的代码中实例化ManagementEventWatcher对象。测试退出时会发出警告: “System.AppDomainUnloadexception:试图访问已卸载的 如果测试启动了一个线程但没有启动,则可能发生这种情况 停止它。确保测试启动的所有线程都已启动 在完成之前停止。” 谁能告诉我如何清除错误或我做错了什么 using System.Management; using Microsoft.VisualStudio.TestTools.UnitTesting; p

我正在尝试单元测试的代码中实例化ManagementEventWatcher对象。测试退出时会发出警告:

“System.AppDomainUnloadexception:试图访问已卸载的 如果测试启动了一个线程但没有启动,则可能发生这种情况 停止它。确保测试启动的所有线程都已启动 在完成之前停止。”

谁能告诉我如何清除错误或我做错了什么

using System.Management;
using Microsoft.VisualStudio.TestTools.UnitTesting;

public class EventWatcherPolling
{
    static int Main(string[] args)
    {
        RunEventWatcher();
        return 0;
    }

    public static void RunEventWatcher()
    {
        ManagementEventWatcher watcher = new ManagementEventWatcher();
        watcher.Stop();
        watcher.Dispose();
    }
}

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        EventWatcherPolling.RunEventWatcher();
    }
}

对于该错误,您通常无能为力:垃圾收集器似乎没有时间收集观察程序,因此您可以尝试在测试运行后等待。您想测试什么?一个在其构造函数(不是我的设计…)中实例化ManagementEventWatcher的类。我想依赖注入是前进的方向。