如果一次运行一个,则单元测试通过良好,如果运行FileLoadException“;溶液中的所有测试”;
本文关键字:如果 运行 测试 一个 一次 单元测试 FileLoadException | 更新日期: 2023-09-27 18:24:49
我试图对我的一些类进行单元测试,但遇到了一个问题,即单独运行测试100%都能正常工作,但如果我批量运行它们/使用"解决方案中的所有测试"选项,我的一个文件的每个测试都会失败,并返回错误:
System.IO.FileLoadException was unhandled by user code
Message=Could not load file or assembly 'Microsoft.Practices.Prism, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Source=ServicesModuleTests
FileName=Microsoft.Practices.Prism, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null
很长一段时间以来,我一直在试图找出原因,并尝试在网上搜索,但没有找到有同样问题的人。
下面是我的代码的一个快速示例:
RegistryService文件:
public class RegistryService
{
protected ILoggerFacadeExtended _Logger { get; set; }
protected IConnectivityService _Connectivity { get; set; }
[ImportingConstructor]
public RegistryService(ILoggerFacadeExtended logger, IConnectivityService connectivity)
{
this._Logger = logger;
this._Connectivity = connectivity;
}
public string GetRegistryPath(RegistryHive hive, string path)
{
string registryPath = string.Format("{0}''{1}", GetRegistryHiveString(hive), path.Trim(''''));
_Logger.DebugWithFormat("Found registry path: {0}", registryPath);
return registryPath;
}
private string GetRegistryHiveString(RegistryHive hive)
{
switch (hive)
{
case RegistryHive.ClassesRoot:
return "HKEY_CLASSES_ROOT";
case RegistryHive.CurrentConfig:
return "HKEY_CURRENT_CONFIG";
case RegistryHive.CurrentUser:
return "HKEY_CURRENT_USER";
case RegistryHive.DynData:
return "HKEY_DYN_DATA";
case RegistryHive.LocalMachine:
return "HKEY_LOCAL_MACHINE";
case RegistryHive.PerformanceData:
return "HKEY_PERFORMANCE_DATA";
case RegistryHive.Users:
return "HKEY_USERS";
}
throw new ArgumentOutOfRangeException("hive");
}
}
测试文件:
private RegistryService CreateMockedRegistryService()
{
return new RegistryService(MockRepository.GenerateMock<ILoggerFacadeExtended>(), MockRepository.GenerateMock<IConnectivityService>());
}
[TestMethod()]
public void GetRegistryPathTest_ClassesRoot()
{
RegistryService target = CreateMockedRegistryService();
RegistryHive hive = RegistryHive.ClassesRoot;
string path = @"Something'SomethingElse'";
string expected = @"HKEY_CLASSES_ROOT'Something'SomethingElse";
string actual;
actual = target.GetRegistryPath(hive, path);
Assert.AreEqual(expected, actual);
}
[TestMethod()]
public void GetRegistryPathTest_CurrentConfig()
{
RegistryService target = CreateMockedRegistryService();
RegistryHive hive = RegistryHive.CurrentConfig;
string path = @"Something'SomethingElse'";
string expected = @"HKEY_CURRENT_CONFIG'Something'SomethingElse";
string actual;
actual = target.GetRegistryPath(hive, path);
Assert.AreEqual(expected, actual);
}
我简化了代码,试图在不占用太多空间的情况下展示我正在做的事情。我可以一个接一个地运行这些程序,但在一起运行时会收到异常。
我通过卸载其他单元测试项目,然后运行测试直到它起作用来解决这个问题,显然其中一个项目使用了一些旧的棱镜引用,并且这些文件被包括在内,而不是更新的文件,在我删除并读取引用后,这个问题现在得到了解决。