2013-03-28 28 views
3

我使用Visual Studio 2010 SP1和包嚮導來爲擴展生成單元測試。單元測試通過,但集成測試(未修改)在帶有NullReferenceException的UIThreadInvoker.Invoke調用中失敗。我在嚮導中添加了一個菜單和一個工具窗口,並帶有複選框。Package Wizard的集成測試中的NullReferenceException

試驗方法VSPackage2_IntegrationTests.IntegrationTests.CSharpProjectTests.WinformsApplication拋出異常: System.NullReferenceException:未設置爲一個對象的實例對象引用。at f:\ dd \ VSSDK \ VSIntegration \ Tools \ src \ IDEHostAdapter \ Framework \ UIThreadInvoker.cs中的Microsoft.VSSDK.Tools.VsIdeTesting.UIThreadInvoker.Invoke(委託方法):第44行 位於VSPackage2_IntegrationTests.IntegrationTests.CSharpProjectTests.WinformsApplication ()在CSharpProjectTests.cs:行62

using System; 
using System.Text; 
using System.Collections.Generic; 
using Microsoft.VisualStudio.TestTools.UnitTesting; 
using Microsoft.VsSDK.IntegrationTestLibrary; 
using Microsoft.VSSDK.Tools.VsIdeTesting; 

namespace VSPackage2_IntegrationTests.IntegrationTests 
{ 
    [TestClass] 
    public class CSharpProjectTests 
    { 
     #region fields 
     private delegate void ThreadInvoker(); 
     private TestContext _testContext; 
     #endregion 

    #region properties 
    /// <summary> 
    ///Gets or sets the test context which provides 
    ///information about and functionality for the current test run. 
    ///</summary> 
    public TestContext TestContext 
    { 
     get { return _testContext; } 
     set { _testContext = value; } 
    } 
    #endregion 

    #region ctors 
    public CSharpProjectTests() 
    { 
    } 
    #endregion 

    #region Additional test attributes 
    // 
    // You can use the following additional attributes as you write your tests: 
    // 
    // Use ClassInitialize to run code before running the first test in the class 
    // [ClassInitialize()] 
    // public static void MyClassInitialize(TestContext testContext) { } 
    // 
    // Use ClassCleanup to run code after all tests in a class have run 
    // [ClassCleanup()] 
    // public static void MyClassCleanup() { } 
    // 
    // Use TestInitialize to run code before running each test 
    // [TestInitialize()] 
    // public void MyTestInitialize() { } 
    // 
    // Use TestCleanup to run code after each test has run 
    // [TestCleanup()] 
    // public void MyTestCleanup() { } 
    // 
    #endregion 

    [TestMethod] 
    [HostType("VS IDE")] 
    public void WinformsApplication() 
    { 
     UIThreadInvoker.Invoke((ThreadInvoker)delegate() 
     { 
      TestUtils testUtils = new TestUtils(); 

      testUtils.CreateEmptySolution(TestContext.TestDir, "CSWinApp"); 
      Assert.AreEqual<int>(0, testUtils.ProjectCount()); 

      //Create Winforms application project 
      //TestUtils.CreateProjectFromTemplate("MyWindowsApp", "Windows Application", "CSharp", false); 
      //Assert.AreEqual<int>(1, TestUtils.ProjectCount()); 

      //TODO Verify that we can debug launch the application 

      //TODO Set Break point and verify that will hit 

      //TODO Verify Adding new project item to project 

     }); 
    } 

} 

}

沒有來源UIThreadInvoker.cs上看到零發生異常......我一定是做了錯誤的設置,所以我重新安裝SDK無濟於事。我也再次使用包嚮導進行仔細檢查,第二種解決方案獲得相同的錯誤。我曾嘗試評論代表的全部和部分內容,但仍然失敗。所有其他集成測試也會在相同位置出現相同錯誤而失敗。

編輯: 當對這個問題進一步的工作,我已修改測試的初始部分來讀取:

[TestMethod] 
[HostType("VS IDE")] 
public void PackageLoadTest() 
{ 
    UIThreadInvoker.Initialize(); 
    UIThreadInvoker.Invoke((ThreadInvoker)OnThreadInvoker); 
} 

private void OnThreadInvoker() 
{ 
    TestUtils testUtils = new TestUtils(); 

    testUtils.CreateEmptySolution(TestContext.TestDir, "CSWinApp"); 
    .... 
} 

在它到達最終使用VsIdeTestHostContext.ServiceProvider,所述CreateEmptySolution方法調試程序,其一片空白。事實上,VsIdeTestHostContext似乎是未初始化的。但是我找不到初始化它的方法。出於某種原因,VS IDE主機類型似乎沒有啓動,或者至少沒有及時啓動。

+0

剛用這個來幫助我一個VS 2012包。這工作像一個魅力,感謝張貼。我希望他們能夠在模板中修復它。 –

回答

1

關於你的編輯 - 從README.DOC 「VS IDE主機適配器VS團隊系統(樣本)」

如果VsIdeTestHostContext.Dte或VsIdeTestHostContext.ServiceProvider 返回null,請確保您的測試項目已參考 正確版本的 Microsoft.Samples.VisualStudio.TestTools.VsIdeTestHostFramework.dll。 如果您的項目具有強烈簽名 版本的框架程序集的引用,但您已安裝未簽名的 版本的框架程序集,則可以獲得null。

+0

這幫助我解決了這個問題。但是,有些依賴性被報告爲缺失 - 直到我將VSXI包的安裝目標升級到當前使用的Visual Studio版本(在這種情況下爲2015) – Marcel

0

除了奧馬爾雷維吾的ecellent暗示,我都經歷過,我需要

升級VSXI包目前使用的Visual Studio版本的安裝目標(2015年在這種情況下)

然後,它的工作。