MSTest vS StructureMap:某些间接引用的 dll 不会被结构图扫描,即使它们确实存在于磁盘上

MSTest vS StructureMap: Certain indirectly-referenced dlls are not getting scanned by structure-map even though they do exist on disk

这里的第一件事是 link 演示项目,展示手头的问题:

https://github.com/dsidirop/MTestStructureMapIssue.git

简而言之,当测试从 Visual Studio 中获取 运行 时,一切都很好。如果我们尝试使用 MTest.exe 运行 相同的测试,如下所示:

"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\MsTest.exe"    /nologo  "/testcontainer:MTestStructureMapIssue.Tests.dll"

然后我们在test-class 'DummyTest'的构造函数中得到以下错误:

 StructureMap.StructureMapConfigurationException: No default Instance is registered and cannot be automatically determined for type 'MTestStructureMapIssue.Dummy.Contracts.ISomeDbContext'

 There is no configuration specified for MTestStructureMapIssue.Dummy.Contracts.ISomeDbContext

 1.) Container.GetInstance<MTestStructureMapIssue.Dummy.Contracts.ISomeDbContext>()

    at StructureMap.SessionCache.GetDefault(Type pluginType, IPipelineGraph pipelineGraph)
    at StructureMap.Container.GetInstance[T]()
    at MTestStructureMapIssue.Tests.DummyTest..ctor() in C:\VS\MTestStructureMapIssue\Code\MTestStructureMapIssue.Tests\DummyTest.cs:line 23.

此错误消息背后的罪魁祸首似乎是 MSTest.exe 出于某种原因禁止扫描某些 dll,因为它甚至不将它们复制到 "test-folder" 尽管事实上我们请使用属性:

[DeploymentItem(@"MTestStructureMapIssue.Dummy.dll")]

如何优雅地克服这一问题?

我找到了一种解决方法来满足 MSTest 的需求(考虑到 [DeploymentItem] 无法解决问题):

 public DummyTest()
 {
        foreach (var file in Directory.GetFiles("../../.."))
        {
            try
            {
                File.Copy(file, Path.Combine(".", Path.GetFileName(file)));
            }
            catch
            {
            }
        }
...

此代码段会将 "bin" 目录中的任何和所有文件(dll、配置等)复制到 MSTest.exe 使用的测试文件夹。