2011-10-10 72 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.