Angular: TestComponentBuilder.createAsync () não resolve sua promessa

Criado em 7 mai. 2016  ·  3Comentários  ·  Fonte: angular/angular

Dado um componente MyComponent expondo uma API simplista para fins de demonstração e importando os símbolos necessários para conduzir os testes de componentes usuais:

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

Tento conduzir um teste básico injetando e instanciando o TestComponentBuilder para criar um componente de fixação do tipo MyComponent , assim:

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));
  });

Sempre recebo o mesmo erro depois de atualizar meu código para RC.1.

Error: No precompiled component MyComponent found

Basicamente, a promessa retornada por testComponentBuilder.createAsync(MyComponent) nunca é resolvida. O problema é consistente em todos os meus testes, abrangendo o uso de TestComponentBuilder .

Para registro, e antes de conduzir qualquer teste, eu configuro previamente os provedores de teste desta forma:

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
);

Eu fiz a varredura do código alto e baixo para determinar se este é um bug real no RC.1 ou um problema com minha configuração de teste, sem sucesso. Antes do RC.1, minha configuração de teste funcionava perfeitamente em todas as especificações dos componentes de teste.

Quaisquer dicas?

Comentários muito úteis

Consegui consertar isso sozinho. Acontece que a configuração dos meus provedores de teste de aplicativo e plataforma estava errada. Configurar o script de configuração como este resolveu o problema:

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
  ]
);

Foi mal! ;)

Todos 3 comentários

Consegui consertar isso sozinho. Acontece que a configuração dos meus provedores de teste de aplicativo e plataforma estava errada. Configurar o script de configuração como este resolveu o problema:

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
  ]
);

Foi mal! ;)

@deeleman Estou tendo o mesmo problema com a configuração padrão usada pelo ng-cli e 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);

Este problema foi bloqueado automaticamente devido à inatividade.
Registre um novo problema se você estiver encontrando um problema semelhante ou relacionado.

Leia mais sobre nossa política de bloqueio automático de conversas .

_Esta ação foi executada automaticamente por um bot._

Esta página foi útil?
0 / 5 - 0 avaliações