3

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(デリゲート メソッド): VSPackage2_IntegrationTests.IntegrationTests.CSharpProjectTests.WinformsApplication() の 44 行目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

        });
    }

}

}

null 例外が発生する場所を確認するための UIThreadInvoker.cs のソースはありません...セットアップで何か間違ったことをしたに違いないので、SDK を再インストールしても無駄でした。また、パッケージ ウィザードを再度使用して再確認しましたが、2 番目の解決策でも同じエラーが発生します。デリゲートのコンテンツのすべてと一部をコメントアウトしようとしましたが、それでも失敗します。他のすべての統合テストも、同じ場所で同じエラーで失敗します。

編集: この問題にさらに取り組んで、テストの最初の部分を次のように変更しました。

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

デバッガーでは、最終的に null である VsIdeTestHostContext.ServiceProvider を使用する CreateEmptySolution メソッドに到達します。実際、VsIdeTestHostContext は初期化されていないように見えます。ただし、初期化する方法が見つかりません。何らかの理由で、VS IDE ホスト タイプが起動していないように見えるか、少なくとも間に合わないようです。

4

2 に答える 2

1

編集について - 「VS Team System用VS IDEホストアダプタ(サンプル)」のReadMe.docより

VsIdeTestHostContext.Dte または VsIdeTestHostContext.ServiceProvider が null を返す場合は、テスト プロジェクトが正しいバージョンの Microsoft.Samples.VisualStudio.TestTools.VsIdeTestHostFramework.dll への参照を持っていることを確認してください。プロジェクトに強力に署名されたバージョンのフレームワーク アセンブリへの参照があるが、署名されていないバージョンのフレームワーク アセンブリがインストールされている場合は、null を取得できます。

于 2014-11-10T08:55:58.397 に答える