单元测试 Unity 配置文件 "The type name or alias xxxx could not be resolved"

Unit Testing Unity Configuration File "The type name or alias xxxx could not be resolved"

我想要一个简单的单元测试来验证我的统一配置中的所有类型 xml 是否有效

[TestMethod]
[TestCategory("IntegrationTest")]
[DeploymentItem(@"..\..\..\App\Unity.config", @"MyApp")]
public void VerifyTypeNamesForMyAppUnityConfig()
{       
   var fileMap = new ExeConfigurationFileMap { 
      ExeConfigFilename = @"MyApp\Unity.config" 
   };
   var configuration = ConfigurationManager
       .OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
   var unitySection = (UnityConfigurationSection)configuration.GetSection("unity");
   var container = new UnityContainer();

   container.LoadConfiguration(unitySection);
}

Unity.config在执行应用程序时有效。

我已经引用了测试项目中的所有程序集

我得到这个异常

Test Name:  VerifyTypeNamesForMyAppUnityConfig
Test FullName:  MyApp.Test.UnityTests.VerifyTypeNamesForMyAppUnityConfig
Test Source:    c:\SourceControl\MyAppMerge\MyApp.Test\UnityTests.cs : line 47
Test Outcome:   Failed
Test Duration:  0:00:00.5187389

Result Message: 
Test method MyApp.Test.UnityTests.VerifyTypeNamesForMyAppUnityConfig threw exception: 
System.InvalidOperationException: The type name or alias MyApp.Services.Attachments.AttachmentService, MyApp.Services.Attachments could not be resolved. Please check your configuration file and verify this type name.
Result StackTrace:  
at Microsoft.Practices.Unity.Configuration.ConfigurationHelpers.TypeResolverImpl.ResolveType(String typeNameOrAlias, Boolean throwIfResolveFails)
   at Microsoft.Practices.Unity.Configuration.RegisterElement.GetMappedType()
   at Microsoft.Practices.Unity.Configuration.RegisterElement.ConfigureContainer(IUnityContainer container)
   at Microsoft.Practices.Unity.Configuration.ContainerElement.<>c__DisplayClass1.<ConfigureContainer>b__0(ContainerConfiguringElement element)
   at Microsoft.Practices.ObjectBuilder2.EnumerableExtensions.ForEach[TItem](IEnumerable`1 sequence, Action`1 action)
   at Microsoft.Practices.Unity.Configuration.ContainerElement.ConfigureContainer(IUnityContainer container)
   at Microsoft.Practices.Unity.Configuration.UnityConfigurationSection.Configure(IUnityContainer container, String configuredContainerName)
   at Microsoft.Practices.Unity.Configuration.UnityContainerExtensions.LoadConfiguration(IUnityContainer container, UnityConfigurationSection section, String containerName)
   at Microsoft.Practices.Unity.Configuration.UnityContainerExtensions.LoadConfiguration(IUnityContainer container, UnityConfigurationSection section)
   at MyApp.Test.UnityTests.VerifyTypeNamesForMyAppUnityConfig() in c:\SourceControl\MyAppMerge\MyApp.Test\UnityTests.cs:line 61

如果我进行此更改,测试会通过

var WTF = new List<AttachmentService>();
container.LoadConfiguration(unitySection);

但是这样失败了

//var WTF = new List<AttachmentService>();
container.LoadConfiguration(unitySection);

为什么?

我有数百个统一配置。大多数人都工作。只有少数 类 需要这个 技巧 。我说不出这一小撮 classes/assemblies 有什么不同。唯一的相似之处是程序集只有 1 到 3 类(非常小的程序集)

我检查了 TestResults 文件夹(正如 Alexei 所建议的),它没有程序集!即使它设置为 Copy Local = True.

MSTest 似乎使用反射来确定复制到 TestResults 文件夹的程序集。

有关详细信息,请参阅此 SO TFS not deploying referenced assembly to test dir when on build server

您必须在程序集中引用某些内容(类似于我上面的 技巧

获取所有 dll:

[DeploymentItem(@".")]

或获取一个 dll:

[DeploymentItem(@"YourAssembly.dll")]

注意:您会遇到一些 "The process cannot access the file" 错误,但不要担心它会复制未使用的错误。 (这是问题所在!)