gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:00:36 31 4
gpt4 key购买 nike

我想要一个简单的单元测试来验证我的统一配置 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);

为什么?

我有数百个统一配置。大多数人都工作。只有少数类需要这个小技巧。我无法说出这些少数类/程序集有什么不同。唯一的相似之处是程序集中只有 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")]

注意:您会收到一些“进程无法访问文件”错误,但不要担心它会复制未使用的文件。 (这是问题所在!)

关于c# - 单元测试 Unity 配置文件 "The type name or alias xxxx could not be resolved",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28418834/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com