C# 程序包向导上的NullReferenceException';s集成测试

C# 程序包向导上的NullReferenceException';s集成测试,c#,visual-studio-2010,unit-testing,vsix,vs-extensibility,C#,Visual Studio 2010,Unit Testing,Vsix,Vs Extensibility,我使用VisualStudio2010SP1和包向导为扩展生成单元测试。单元测试通过,但集成测试(未修改)在UIThreadInvoker.Invoke上失败,调用时出现NullReferenceException。我在向导中添加了一个菜单和一个带有复选框的工具窗口 测试方法VSPackage2_IntegrationTests.IntegrationTests.CSharpProjectTests.WinformsApplication引发异常: System.NullReferenceExc

我使用VisualStudio2010SP1和包向导为扩展生成单元测试。单元测试通过,但集成测试(未修改)在UIThreadInvoker.Invoke上失败,调用时出现NullReferenceException。我在向导中添加了一个菜单和一个带有复选框的工具窗口

测试方法VSPackage2_IntegrationTests.IntegrationTests.CSharpProjectTests.WinformsApplication引发异常: System.NullReferenceException:对象引用未设置为对象的实例。 在f:\dd\VSSDK\VSIntegration\Tools\src\IDEHostAdapter\Framework\UIThreadInvoker.cs中的Microsoft.VSSDK.Tools.VsIdeTesting.UIThreadInvoker.Invoke(委托方法)处:第44行 在CSharpProjectTests.cs中的VSPackage2_IntegrationTests.IntegrationTests.CSharpProjectTests.WinformsApplication()中:第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

        });
    }

}

在调试器中,它到达CreateEmptySolution方法,该方法最终使用VsIdeTestHostContext.ServiceProvider,该方法为null。事实上,VsIdeTestHostContext似乎未初始化。但是,我找不到初始化它的方法。由于某些原因,VS IDE主机类型似乎没有启动,或者至少没有及时启动。

关于您的编辑-来自ReadMe.doc的“VS团队系统VS IDE主机适配器(示例)”

如果VsIdeTestHostContext.Dte或VsIdeTestHostContext.ServiceProvider 返回null,请确保您的测试项目引用了 正确版本的 Microsoft.Samples.VisualStudio.TestTools.VsIdeTestHostFramework.dll。 如果您的项目有对强签名的 框架程序集的版本,但已安装未签名的 框架程序集的版本


除了奥马尔·拉维夫(Omer Raviv)的暗示之外,我也经历过,我需要这样做

将VSXI软件包的安装目标升级到当前使用的Visual Studio版本(本例中为2015版)


然后,它成功了。

只是用它来帮助我完成VS 2012软件包。这很有魅力,谢谢你的发帖。我希望他们能在模板中修复它。这帮助我克服了这个问题。但是,一些依赖项被报告为缺失,直到我将VSXI包的安装目标升级到当前使用的Visual Studio版本(本例中为2015年)
[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");
    ....
}