2011-10-10 109 views
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”的类型初始值设定项引发了一个异常。

内部异常:

无法加载文件或程序:先证者,版本= 1.0.0.0,文化=中立,公钥=空...

有没有人如何在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(); 
    } 

我绝对不明白,为什么“新PrivateType(“先证者,版本......”不起作用。有没有人的想法?

+0

什么是'Proband'装配,以及如何你有* *试图使其可用? –

+0

你到底在写什么?似乎你使用MSTest框架,从你使用[TestMethod]。你正在写某种自定义测试_runner_吗? –

+0

哦,我很抱歉没有解释:proband程序集是包含应该测试的代码的程序集。 ExampleMethods是proband程序集中的一个类,它包含私有方法(“SomeMethod”)。 – Christian

回答

1

我已成功地创建用于问题的解决方法。

为了我的AppDomain中,我加入了一个AssemblyResolv eEventHandler:

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; 
     } 
    } 

现在的代码行 “目标=新ExampleMethods_Accessor();” 工作正常,并返回正确的访问对象。

我还是不明白,为什么Assembly不能自动解析。

即使是不太可能,任何人都会有同样的问题:我希望这个答案可以帮助别人:)

0

我没有做任何事情几乎一样复杂,但我有:

  1. 网使用.NET 3的应用程序项目。通过使用.net 3.5

试图运行使用访问一个单元测试时,我得到了相同的BadImageFormat例外.NET 3.5

  • 测试项目5
  • 配置项目。

    我发现下面的链接:

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

    第二变通解决我的问题。我改变了测试项目的目标.NET 4.0,它的工作。

  • +0

    感谢您的输入,我将使用.NET 4.0测试程序集来尝试此操作。 – Christian

    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.