【发布时间】:2014-06-23 12:44:41
【问题描述】:
我编写了一个插件库,可以在目录中搜索导出给定接口实现的程序集。它通过将所有程序集加载到仅反射上下文中的临时应用程序域并搜索导出的类型来实现此目的。然后卸载临时应用程序域,感兴趣的程序集加载到默认应用程序域中,然后实例化找到的类型的对象以通过搜索接口使用。
我正在尝试使用 NUnit 为该流程编写一些单元测试,但在尝试连接 ReflectionOnlyAssemblyResolve 事件时,我仅在单元测试中收到 FileNotFoundException。这是一个简单的例子:
using System;
using System.Reflection;
using NUnit.Framework;
namespace NUnitAppDomains
{
[TestFixture]
public class TestClass
{
[Test]
public void NUnitTest()
{
var ad = AppDomain.CreateDomain("someName");
// this next line throws a FileNotFoundException, complaining about not being
// able to find the test assembly itself... o.O
ad.ReflectionOnlyAssemblyResolve += SomeHandler;
AppDomain.Unload(ad);
}
static Assembly SomeHandler(object sender, ResolveEventArgs args)
{
// some code would be here
throw new NotImplementedException();
}
}
}
我试图在已知位置的一些虚拟程序集上测试代码,其中一些包含/不包含有效的实施/s。我的代码是否不适合单元测试,或者如果是,我该如何避免这些异常?谢谢
【问题讨论】:
-
格式良好的问题,感谢SSCCE。欢迎来到 SO!
标签: c# unit-testing plugins nunit appdomain