Angular: TestComponentBuilder.createAsync()はその約束を解決しません

作成日 2016年05月07日  ·  3コメント  ·  ソース: angular/angular

コンポーネントMyComponentがデモ目的で単純なAPIを公開し、通常のコンポーネントテストを実行するために必要なシンボルをインポートするとします。

import MyComponent from './my.component';
import {
  describe,
  expect,
  it,
  inject,
  beforeEach,
  beforeEachProviders } from '@angular/core/testing';
import { TestComponentBuilder } from '@angular/compiler/testing';

TestComponentBuilderを挿入してインスタンス化する基本的なテストを実行して、次のようなMyComponentタイプのフィクスチャコンポーネントを作成しようとしています。

describe('MyComponent', () => {
  let testComponentBuilder: TestComponentBuilder;

  beforeEachProviders(() => [TestComponentBuilder]);

  beforeEach(inject([TestComponentBuilder],  (_testComponentBuilder: TestComponentBuilder) => {
      testComponentBuilder = _testComponentBuilder;
    }
  ));

  it('should feature this or that behavior', done => {
    // We create a test component fixture on runtime out from the component symbol
    testComponentBuilder.createAsync(MyComponent).then(componentFixture => {

      // Here we proceed to run assertions on the componentFixture
      // ...

      done();
    })
    .catch(e => done.fail(e));
  });

コードをRC.1にアップグレードした後、常に同じエラーが発生します。

Error: No precompiled component MyComponent found

基本的に、 testComponentBuilder.createAsync(MyComponent)によって返される約束は決して解決されません。 この問題は、 TestComponentBuilder使用を含むすべてのテストで一貫しています。

記録のために、そしてテストを実施する前に、私は以前に次のようにテストプロバイダーを構成しました:

import { setBaseTestProviders } from '@angular/core/testing';
import {
  TEST_BROWSER_STATIC_PLATFORM_PROVIDERS,
  TEST_BROWSER_STATIC_APPLICATION_PROVIDERS
} from '@angular/platform-browser/testing';

setBaseTestProviders(
  TEST_BROWSER_STATIC_PLATFORM_PROVIDERS,
  TEST_BROWSER_STATIC_APPLICATION_PROVIDERS
);

コードを高低でスキャンして、これがRC.1の実際のバグなのか、テストセットアップの問題なのかを判断しましたが、成功しませんでした。 RC.1より前は、私のテストセットアップは、特にすべての仕様のテストコンポーネントでシームレスに機能していました。

ポインタはありますか?

最も参考になるコメント

私はこれを自分で修正することができました。 私のアプリケーションとプラットフォームテストプロバイダーのセットアップが間違っていたことがわかりました。 このようにセットアップスクリプトを構成すると、次のようになります。

import { resetBaseTestProviders, setBaseTestProviders } from '@angular/core/testing';
import { BROWSER_APP_DYNAMIC_PROVIDERS } from "@angular/platform-browser-dynamic";
import {
  TEST_BROWSER_STATIC_PLATFORM_PROVIDERS,
  ADDITIONAL_TEST_BROWSER_PROVIDERS
} from '@angular/platform-browser/testing';

resetBaseTestProviders();
setBaseTestProviders(
  TEST_BROWSER_STATIC_PLATFORM_PROVIDERS,
  [
    BROWSER_APP_DYNAMIC_PROVIDERS,
    ADDITIONAL_TEST_BROWSER_PROVIDERS
  ]
);

私の悪い! ;)

全てのコメント3件

私はこれを自分で修正することができました。 私のアプリケーションとプラットフォームテストプロバイダーのセットアップが間違っていたことがわかりました。 このようにセットアップスクリプトを構成すると、次のようになります。

import { resetBaseTestProviders, setBaseTestProviders } from '@angular/core/testing';
import { BROWSER_APP_DYNAMIC_PROVIDERS } from "@angular/platform-browser-dynamic";
import {
  TEST_BROWSER_STATIC_PLATFORM_PROVIDERS,
  ADDITIONAL_TEST_BROWSER_PROVIDERS
} from '@angular/platform-browser/testing';

resetBaseTestProviders();
setBaseTestProviders(
  TEST_BROWSER_STATIC_PLATFORM_PROVIDERS,
  [
    BROWSER_APP_DYNAMIC_PROVIDERS,
    ADDITIONAL_TEST_BROWSER_PROVIDERS
  ]
);

私の悪い! ;)

@deeleman ng-cliとhttps://github.com/juliemr/ng2-test-seed/blob/master/karma-test-shim.jsで使用されるデフォルトのセットアップで、これと同じ問題が発生してい

/*global jasmine, __karma__, window*/
Error.stackTraceLimit = Infinity;
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000;

__karma__.loaded = function () {
};

var distPath = '/base/.test/';
var appPath = distPath + 'app/';

function isJsFile(path) {
  return path.slice(-3) == '.js';
}

function isSpecFile(path) {
  return path.slice(-8) == '.spec.js';
}

function isAppFile(path) {
  return isJsFile(path) && (path.substr(0, appPath.length) == appPath);
}

var allSpecFiles = Object.keys(window.__karma__.files)
  .filter(isSpecFile)
  .filter(isAppFile);

// Load our SystemJS configuration.
System.config({
  baseURL: distPath,
});


System.import('system.config.js').then(function(e) {
  // Load and configure the TestComponentBuilder.
  return Promise.all([
    System.import('@angular/core/testing'),
    System.import('@angular/platform-browser-dynamic/testing')
  ]).then(function (providers) {
    var testing = providers[0];
    var testingBrowser = providers[1];
    testing.setBaseTestProviders(
      testingBrowser.TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
      testingBrowser.TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS
    );
  });
}).then(function() {
  return Promise.all(
    allSpecFiles.map(function (moduleName) {
      return System.import(moduleName);
    }));
}).then(__karma__.start, __karma__.error);

この問題は、非アクティブのために自動的にロックされています。
同様の問題または関連する問題が発生した場合は、新しい問題を提出してください。

自動会話ロックポリシーの詳細をご覧ください。

_このアクションはボットによって自動的に実行されました。_

このページは役に立ちましたか?
0 / 5 - 0 評価