5

我目前正在编写一个单元测试框架,最终将运行用 Visual Studio 编写的标准单元测试。该框架当前无法与访问器一起正常工作。考虑以下测试方法:

[TestMethod()]
public void TestMethod()
{
      ExampleMethods_Accessor target = null;
      target = new ExampleMethods_Accessor();
      target.SomeMethod();
}

在此示例中,访问器已由 Visual Studio 生成。使用 Visual Studio 的单元测试环境运行时,单元测试工作得非常好。但是,我想从我的框架中调用 TestMethod()。在“target = new ExampleMethods_Accessor()”行,抛出以下异常:

“Proband.ExampleMethods_Accessor”的类型初始化程序引发了异常。

内部异常:

无法加载文件或程序集:Proband,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null...

有人知道 Microsoft 单元测试框架如何调用单元测试吗?我在想这可能是由于缺少 TestContext 对象。就我而言,这是“空”。在 Visual Studio 中启动单元测试时,TestContext 对象包含大量信息。可能是我需要正确初始化它吗?它需要如何初始化?

感谢所有的帮助,克里斯蒂安

编辑:

我一直在尝试访问器的工作方式。我使用 ILSpy 查看 Proband_Accessor.dll 中生成了哪些代码。事实证明,导致异常的指令是:

SomeClass_Accessor.m_privateType = new PrivateType("Probant, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "Probant.SomeClass");

我将单元测试代码修改为这样(仅用于测试):

    [TestMethod()]
    [DeploymentItem("Proband.dll")]
    public void SomeMethodTest()
    {
        ExampleMethods_Accessor target = null;
        ExampleMethods c = null;

        try
        {
            Assembly.Load("Proband, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"); // this works fine
            PrivateType tx = new PrivateType(typeof(ExampleMethods)); // this works fine as well (also without loading the assembly)

            PrivateType t = new PrivateType("Proband, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "Proband.ExampleMethods"); // this causes the exception

            c = new ExampleMethods(); // this works fine
            target = new ExampleMethods_Accessor(); // this causes the exception as well
        }
        catch (Exception ex)
        {
            Console.WriteLine();
        }
        int actual;
        actual = target.SomeMethod();
    }

我完全不明白,为什么“new PrivateType(”Proband,Version ....)不起作用。有人知道吗?

4

3 回答 3

1

我已经设法为这个问题创建了一个解决方法。

在我的 AppDomain 中,我添加了一个 AssemblyResolveEventHandler:

    AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);

此事件处理程序包含以下代码:

    private Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)
    {
        if(args.Name == "Proband, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")
        {
            // resolving correct assembly of type under test
            return typeof(ExampleMethods).Assembly;
        }
        else
        {
            return null;
        }
    }

现在代码行“target = new ExampleMethods_Accessor();” 工作正常并返回正确的访问器对象。

我还是不明白,为什么不能自动解决大会。

即使任何人都不太可能遇到同样的问题:我希望这个答案可以帮助某人:)

于 2011-10-11T10:44:06.760 回答
0

我没有做任何复杂的事情,但我有:

  1. 使用 .NET 3.5 的 Web 应用程序项目
  2. 使用 .NET 3.5 的配置项目
  3. 使用 .NET 3.5 测试项目

尝试使用访问器运行单元测试时,我遇到了相同的 BadImageFormat 异常。

我找到了以下链接:

http://connect.microsoft.com/VisualStudio/feedback/details/677203/even-after-installing-vs2010-sp1-unit-tests-targeting-3-5-framework-fail-if-they-are-using-私有访问器#details

第二个解决方法解决了我的问题。我将测试项目更改为针对 .NET 4.0 并且它有效。

于 2012-03-15T16:17:12.713 回答
0

我刚刚遇到了这个确切的问题,这是因为我DeploymentItem从测试方法中删除了该属性。一旦我再次添加它,我不再在构建机器上收到错误。

[TestMethod]
[DeploymentItem("FedImportServer.dll")]  // ** This is necessary for the build machine. **
public void SourceFileStillExistsAfterProcessingFails()

注意:我在本地运行时从未遇到过错误。

这是错误:

Test method FedImportTests.FedImportServiceHostTest.FileNoLongerExistsAfterSucessfulProcessing threw exception: System.TypeInitializationException: The type initializer for 'FedImportServer.Processing.FileProcessor_Accessor' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'FedImportServer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

于 2012-07-30T21:17:40.580 回答