我使用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主機類型似乎沒有啓動,或者至少沒有及時啓動。
剛用這個來幫助我一個VS 2012包。這工作像一個魅力,感謝張貼。我希望他們能夠在模板中修復它。 –