c# - Package Wizard 集成测试中的 NullReferenceException

标签 c# visual-studio-2010 unit-testing vsix vs-extensibility

我使用 Visual Studio 2010 SP1 和包向导为扩展生成单元测试。单元测试通过但集成测试(未修改)在 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(委托(delegate)方法):第 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

        });
    }

}

没有 UIThreadInvoker.cs 的来源来查看 null 异常发生的位置......我一定是在设置中做错了什么所以我重新安装了 SDK 无济于事。我还再次使用包向导仔细检查,第二个解决方案得到相同的错误。我已经尝试注释掉委托(delegate)的全部和部分内容,但它仍然失败。所有其他集成测试也因同一位置的相同错误而失败。

编辑: 在进一步研究这个问题后,我将测试的初始部分修改为:

[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");
    ....
}

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

最佳答案

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

If VsIdeTestHostContext.Dte or VsIdeTestHostContext.ServiceProvider returns null, make sure that your test project has reference to correct version of Microsoft.Samples.VisualStudio.TestTools.VsIdeTestHostFramework.dll. You can get null if your project has a reference to strongly signed version of the framework assembly but you have installed unsigned version of the framework assembly.

关于c# - Package Wizard 集成测试中的 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15687260/

相关文章:

c# - 返回对象的实例

visual-studio-2010 - VS2010 C++ 如何为每个开发人员提供不同的项目引用

ruby-on-rails - 哪里用 `FactoryGirl.build_stubbed` 哪里用RSpec的 `mock`/`double`

javascript - 模拟来自同一个类的 promise 返回方法

ruby-on-rails - 为什么记录在我的 Rails 应用程序中消失了?

c# - 无法打开 mysql 连接

c# - 引用另一个程序集中的类型导致 Razor.Compile() 中出现 "the type is defined in an assembly that is not referenced"错误

C# LINQ 问题,为什么这里需要 new?

c# - 访问 Datagridview (WinForms) 中的不可见列

vb.net - 你怎么知道你的解决方案是从哪里打开的?