编程语言
首页 > 编程语言> > c# – 如果一次运行一个单元测试则传递正常,如果运行“解决方案中的所有测试”,则为FileLoadException

c# – 如果一次运行一个单元测试则传递正常,如果运行“解决方案中的所有测试”,则为FileLoadException

作者:互联网

我正在尝试对我的一些类进行单元测试并遇到一个问题,即单独运行测试可以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);
}

我愚蠢的代码试图在不占用太多空间的情况下展示我正在做的事情.我可以一个接一个地运行这些,但是在一起运行时会收到异常.

解决方法:

我通过卸载其他单元测试项目然后运行测试直到它工作来解决它,显然其中一个使用了一些较旧的棱镜引用并且这些文件被包含而不是更新的文件,这个问题现在在我删除并重新读取后解决了参考文献.

标签:c,unit-testing,visual-studio-2010,rhino-mocks,prism
来源: https://codeday.me/bug/20190709/1416539.html